xref: /titanic_51/usr/src/cmd/valtools/ckyorn.c (revision 1bdd6c0e3710e91cb1f31aa78de33cb638494480)
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 <limits.h>
40 #include "usage.h"
41 #include "libadm.h"
42 
43 #define	BADPID	(-2)
44 
45 static char	*prog;
46 static char	*deflt, *prompt, *error, *help;
47 static int	kpid = BADPID;
48 static int	signo;
49 
50 static const char	husage[] = "Wh";
51 static const char	eusage[] = "We";
52 
53 static void
54 usage(void)
55 {
56 	switch (*prog) {
57 	default:
58 		(void) fprintf(stderr,
59 			gettext("usage: %s [options]\n"), prog);
60 		(void) fprintf(stderr, gettext(OPTMESG));
61 		(void) fprintf(stderr, gettext(STDOPTS));
62 		break;
63 
64 	case 'v':
65 		(void) fprintf(stderr,
66 			gettext("usage: %s input\n"), prog);
67 		break;
68 
69 	case 'h':
70 		(void) fprintf(stderr,
71 			gettext("usage: %s [options]\n"), prog);
72 		(void) fprintf(stderr, gettext(OPTMESG));
73 		(void) fprintf(stderr,
74 			gettext("\t-W width\n\t-h help\n"));
75 		break;
76 
77 	case 'e':
78 		(void) fprintf(stderr,
79 			gettext("usage: %s [options]\n"), prog);
80 		(void) fprintf(stderr, gettext(OPTMESG));
81 		(void) fprintf(stderr,
82 			gettext("\t-W width\n\t-e error\n"));
83 		break;
84 	}
85 	exit(1);
86 }
87 
88 /*
89  * Given argv[0], return a pointer to the basename of the program.
90  */
91 static char *
92 prog_name(char *arg0)
93 {
94 	char *str;
95 
96 	/* first strip trailing '/' characters (exec() allows these!) */
97 	str = arg0 + strlen(arg0);
98 	while (str > arg0 && *--str == '/')
99 		*str = '\0';
100 	if ((str = strrchr(arg0, '/')) != NULL)
101 		return (str + 1);
102 	return (arg0);
103 }
104 
105 int
106 main(int argc, char **argv)
107 {
108 	int	c, n;
109 	char	*ynval;
110 	size_t	len;
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, "d:p:e:h:k:s:QW:?")) != EOF) {
122 		/* check for invalid option */
123 		if (*prog == 'v')
124 			usage();
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 'd':
146 			deflt = optarg;
147 			break;
148 
149 		case 'p':
150 			prompt = optarg;
151 			break;
152 
153 		case 'e':
154 			error = optarg;
155 			break;
156 
157 		case 'h':
158 			help = optarg;
159 			break;
160 
161 		case 'k':
162 			kpid = atoi(optarg);
163 			break;
164 
165 		case 's':
166 			signo = atoi(optarg);
167 			break;
168 
169 		default:
170 			usage();
171 		}
172 	}
173 
174 	if (signo) {
175 		if (kpid == BADPID)
176 			usage();
177 	} else
178 		signo = SIGTERM;
179 
180 	if (*prog == 'v') {
181 		if (argc != (optind + 1))
182 			usage();
183 		if (ckyorn_val(argv[optind]))
184 			exit(1);
185 		else
186 			exit(0);
187 	}
188 
189 	if (optind != argc)
190 		usage();
191 
192 	if (*prog == 'e') {
193 		ckindent = 0;
194 		ckyorn_err(error);
195 		exit(0);
196 	} else if (*prog == 'h') {
197 		ckindent = 0;
198 		ckyorn_hlp(help);
199 		exit(0);
200 	}
201 
202 	if (deflt) {
203 		len = strlen(deflt) + 1;
204 		if (len < MAX_INPUT)
205 			len = MAX_INPUT;
206 	} else {
207 		len = MAX_INPUT;
208 	}
209 	ynval = (char *)malloc(len);
210 	if (!ynval) {
211 		(void) fprintf(stderr,
212 			gettext("Not enough memory\n"));
213 		exit(1);
214 	}
215 	n = ckyorn(ynval, deflt, error, help, prompt);
216 	if (n == 3) {
217 		if (kpid > -2) {
218 			if (kill(kpid, signo)) {
219 				(void) fprintf(stderr,
220 			gettext("Failed to send signal %d to process %d\n"),
221 				signo, kpid);
222 			}
223 		}
224 		(void) puts("q");
225 	} else if (n == 0)
226 		(void) fputs(ynval, stdout);
227 	return (n);
228 }
229