xref: /titanic_41/usr/src/cmd/valtools/ckint.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <locale.h>
38 #include <libintl.h>
39 #include "usage.h"
40 #include "libadm.h"
41 
42 #define	BADPID (-2)
43 
44 static char	*prog;
45 static char	*deflt, *prompt, *error, *help;
46 static int	kpid = BADPID;
47 static int	signo;
48 static short	base;
49 
50 static const char	vusage[] = "b";
51 static const char	husage[] = "bWh";
52 static const char	eusage[] = "bWe";
53 
54 static void
55 usage(void)
56 {
57 	switch (*prog) {
58 	default:
59 		(void) fprintf(stderr,
60 			gettext("usage: %s [options] [-b base]\n"), prog);
61 		(void) fprintf(stderr, gettext(OPTMESG));
62 		(void) fprintf(stderr, gettext(STDOPTS));
63 		break;
64 
65 	case 'v':
66 		(void) fprintf(stderr,
67 			gettext("usage: %s [-b base] input\n"), prog);
68 		break;
69 
70 	case 'h':
71 		(void) fprintf(stderr,
72 			gettext("usage: %s [options] [-b base]\n"), prog);
73 		(void) fprintf(stderr, gettext(OPTMESG));
74 		(void) fprintf(stderr,
75 			gettext("\t-W width\n\t-h help\n"));
76 		break;
77 
78 	case 'e':
79 		(void) fprintf(stderr,
80 			gettext("usage: %s [options] [-b base]\n"), prog);
81 		(void) fprintf(stderr, gettext(OPTMESG));
82 		(void) fprintf(stderr,
83 			gettext("\t-W width\n\t-e error\n"));
84 		break;
85 	}
86 	exit(1);
87 }
88 
89 /*
90  * Given argv[0], return a pointer to the basename of the program.
91  */
92 static char *
93 prog_name(char *arg0)
94 {
95 	char *str;
96 
97 	/* first strip trailing '/' characters (exec() allows these!) */
98 	str = arg0 + strlen(arg0);
99 	while (str > arg0 && *--str == '/')
100 		*str = '\0';
101 	if ((str = strrchr(arg0, '/')) != NULL)
102 		return (str + 1);
103 	return (arg0);
104 }
105 
106 int
107 main(int argc, char **argv)
108 {
109 	int	c, n;
110 	long	intval;
111 
112 	(void) setlocale(LC_ALL, "");
113 
114 #if	!defined(TEXT_DOMAIN)
115 #define	TEXT_DOMAIN	"SYS_TEST"
116 #endif
117 	(void) textdomain(TEXT_DOMAIN);
118 
119 	prog = prog_name(argv[0]);
120 
121 	while ((c = getopt(argc, argv, "b:d:p:e:h:k:s:QW:?")) != EOF) {
122 		/* check for invalid option */
123 		if ((*prog == 'v') && !strchr(vusage, c))
124 			usage(); /* no valid options */
125 		if ((*prog == 'e') && !strchr(eusage, c))
126 			usage();
127 		if ((*prog == 'h') && !strchr(husage, c))
128 			usage();
129 
130 		switch (c) {
131 		case 'Q':
132 			ckquit = 0;
133 			break;
134 
135 		case 'W':
136 			ckwidth = atoi(optarg);
137 			if (ckwidth < 0) {
138 				(void) fprintf(stderr,
139 		gettext("%s: ERROR: negative display width specified\n"),
140 					prog);
141 				exit(1);
142 			}
143 			break;
144 
145 		case 'b':
146 			base = atoi(optarg);
147 			if ((base < 2) || (base > 36)) {
148 				(void) fprintf(stderr,
149 		gettext("%s: ERROR: base must be between 2 and 36\n"),
150 					prog);
151 				exit(1);
152 			}
153 			break;
154 
155 		case 'd':
156 			deflt = optarg;
157 			break;
158 
159 		case 'p':
160 			prompt = optarg;
161 			break;
162 
163 		case 'e':
164 			error = optarg;
165 			break;
166 
167 		case 'h':
168 			help = optarg;
169 			break;
170 
171 		case 'k':
172 			kpid = atoi(optarg);
173 			break;
174 
175 		case 's':
176 			signo = atoi(optarg);
177 			break;
178 
179 		default:
180 			usage();
181 		}
182 	}
183 
184 	if (signo) {
185 		if (kpid == BADPID)
186 			usage();
187 	} else
188 		signo = SIGTERM;
189 
190 	if (*prog == 'v') {
191 		if (argc != (optind + 1))
192 			usage();
193 		exit(ckint_val(argv[optind], base));
194 	}
195 
196 	if (optind != argc)
197 		usage();
198 
199 	if (*prog == 'e') {
200 		ckindent = 0;
201 		ckint_err(base, error);
202 		exit(0);
203 	} else if (*prog == 'h') {
204 		ckindent = 0;
205 		ckint_hlp(base, help);
206 		exit(0);
207 	}
208 
209 	n = ckint(&intval, base, deflt, error, help, prompt);
210 	if (n == 3) {
211 		if (kpid > -2)
212 			(void) kill(kpid, signo);
213 		(void) puts("q");
214 	} else if (n == 0)
215 		(void) printf("%ld", intval);
216 	return (n);
217 }
218