xref: /illumos-gate/usr/src/cmd/valtools/ckrange.c (revision e5ab5ea55344704b841f6f500ca3beb83dfb1cd6)
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 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <stdio.h>
31 #include <limits.h>
32 #include <string.h>
33 #include <signal.h>
34 #include <stdlib.h>
35 #include <locale.h>
36 #include <libintl.h>
37 #include "usage.h"
38 #include "libadm.h"
39 
40 #define	BADPID	(-2)
41 
42 static char	*prog;
43 static char	*deflt = NULL, *prompt = NULL, *error = NULL, *help = NULL;
44 static int	kpid = BADPID;
45 static int	signo;
46 static int	base = 10;
47 static char	*upper;
48 static char	*lower;
49 
50 static const char	vusage[] = "bul";
51 static const char	husage[] = "bulWh";
52 static const char	eusage[] = "bulWe";
53 
54 #define	USAGE	"[-l lower] [-u upper] [-b base]"
55 
56 static void
57 usage(void)
58 {
59 	switch (*prog) {
60 	default:
61 		(void) fprintf(stderr,
62 			gettext("usage: %s [options] %s\n"),
63 			prog, USAGE);
64 		(void) fprintf(stderr, gettext(OPTMESG));
65 		(void) fprintf(stderr, gettext(STDOPTS));
66 		break;
67 
68 	case 'v':
69 		(void) fprintf(stderr,
70 			gettext("usage: %s %s input\n"), prog, USAGE);
71 		break;
72 
73 	case 'h':
74 		(void) fprintf(stderr,
75 			gettext("usage: %s [options] %s\n"),
76 			prog, USAGE);
77 		(void) fprintf(stderr, gettext(OPTMESG));
78 		(void) fprintf(stderr,
79 			gettext("\t-W width\n\t-h help\n"));
80 		break;
81 
82 	case 'e':
83 		(void) fprintf(stderr,
84 			gettext("usage: %s [options] %s\n"),
85 			prog, USAGE);
86 		(void) fprintf(stderr, gettext(OPTMESG));
87 		(void) fprintf(stderr,
88 			gettext("\t-W width\n\t-e error\n"));
89 		break;
90 	}
91 	exit(1);
92 }
93 
94 /*
95  * Given argv[0], return a pointer to the basename of the program.
96  */
97 static char *
98 prog_name(char *arg0)
99 {
100 	char *str;
101 
102 	/* first strip trailing '/' characters (exec() allows these!) */
103 	str = arg0 + strlen(arg0);
104 	while (str > arg0 && *--str == '/')
105 		*str = '\0';
106 	if ((str = strrchr(arg0, '/')) != NULL)
107 		return (str + 1);
108 	return (arg0);
109 }
110 
111 int
112 main(int argc, char **argv)
113 {
114 	int	c, n;
115 	long	lvalue, uvalue, intval;
116 	char	*ptr = 0;
117 
118 	(void) setlocale(LC_ALL, "");
119 
120 #if	!defined(TEXT_DOMAIN)
121 #define	TEXT_DOMAIN	"SYS_TEST"
122 #endif
123 	(void) textdomain(TEXT_DOMAIN);
124 
125 	prog = prog_name(argv[0]);
126 
127 	while ((c = getopt(argc, argv, "l:u:b:d:p:e:h:k:s:QW:?")) != EOF) {
128 		/* check for invalid option */
129 		if ((*prog == 'v') && !strchr(vusage, c))
130 			usage();
131 		if ((*prog == 'e') && !strchr(eusage, c))
132 			usage();
133 		if ((*prog == 'h') && !strchr(husage, c))
134 			usage();
135 
136 		switch (c) {
137 		case 'Q':
138 			ckquit = 0;
139 			break;
140 
141 		case 'W':
142 			ckwidth = atoi(optarg);
143 			if (ckwidth < 0) {
144 				(void) fprintf(stderr,
145 		gettext("%s: ERROR: negative display width specified\n"),
146 					prog);
147 				exit(1);
148 			}
149 			break;
150 
151 		case 'b':
152 			base = atoi(optarg);
153 			if ((base < 2) || (base > 36)) {
154 				(void) fprintf(stderr,
155 		gettext("%s: ERROR: base must be between 2 and 36\n"),
156 					prog);
157 				exit(1);
158 			}
159 			break;
160 
161 		case 'u':
162 			upper = optarg;
163 			break;
164 
165 		case 'l':
166 			lower = optarg;
167 			break;
168 
169 		case 'd':
170 			deflt = optarg;
171 			break;
172 
173 		case 'p':
174 			prompt = optarg;
175 			break;
176 
177 		case 'e':
178 			error = optarg;
179 			break;
180 
181 		case 'h':
182 			help = optarg;
183 			break;
184 
185 		case 'k':
186 			kpid = atoi(optarg);
187 			break;
188 
189 		case 's':
190 			signo = atoi(optarg);
191 			break;
192 
193 		default:
194 			usage();
195 		}
196 	}
197 
198 	if (signo) {
199 		if (kpid == BADPID)
200 			usage();
201 	} else
202 		signo = SIGTERM;
203 
204 	if (upper) {
205 		uvalue = strtol(upper, &ptr, base);
206 		if (ptr == upper) {
207 			(void) fprintf(stderr,
208 		gettext("%s: ERROR: invalid upper value specification\n"),
209 				prog);
210 			exit(1);
211 		}
212 	} else
213 		uvalue = LONG_MAX;
214 	if (lower) {
215 		lvalue =  strtol(lower, &ptr, base);
216 		if (ptr == lower) {
217 			(void) fprintf(stderr,
218 		gettext("%s: ERROR: invalid lower value specification\n"),
219 				prog);
220 			exit(1);
221 		}
222 	} else
223 		lvalue = LONG_MIN;
224 
225 	if (uvalue < lvalue) {
226 		(void) fprintf(stderr,
227 		gettext("%s: ERROR: upper value is less than lower value\n"),
228 			prog);
229 		exit(1);
230 	}
231 
232 	if (*prog == 'v') {
233 		if (argc != (optind+1))
234 			usage();
235 		exit(ckrange_val(lvalue, uvalue, base, argv[optind]));
236 	}
237 
238 	if (optind != argc)
239 		usage();
240 
241 	if (*prog == 'e') {
242 		ckindent = 0;
243 		ckrange_err(lvalue, uvalue, base, error);
244 		exit(0);
245 	} else if (*prog == 'h') {
246 		ckindent = 0;
247 		ckrange_hlp(lvalue, uvalue, base, help);
248 		exit(0);
249 	}
250 
251 	n = ckrange(&intval, lvalue, uvalue, (short)base,
252 		deflt, error, help, prompt);	/* libadm interface */
253 	if (n == 3) {
254 		if (kpid > -2)
255 			(void) kill(kpid, signo);
256 		(void) puts("q");
257 	} else if (n == 0)
258 		(void) printf("%ld", intval);
259 	return (n);
260 }
261