8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / valtools / ckyorn.c
blob983967565ba1da17d603cb8c7ac255cbba9c3b45
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
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"
43 #define BADPID (-2)
45 static char *prog;
46 static char *deflt, *prompt, *error, *help;
47 static int kpid = BADPID;
48 static int signo;
50 static const char husage[] = "Wh";
51 static const char eusage[] = "We";
53 static void
54 usage(void)
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;
64 case 'v':
65 (void) fprintf(stderr,
66 gettext("usage: %s input\n"), prog);
67 break;
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;
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;
85 exit(1);
89 * Given argv[0], return a pointer to the basename of the program.
91 static char *
92 prog_name(char *arg0)
94 char *str;
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);
106 main(int argc, char **argv)
108 int c, n;
109 char *ynval;
110 size_t len;
112 (void) setlocale(LC_ALL, "");
114 #if !defined(TEXT_DOMAIN)
115 #define TEXT_DOMAIN "SYS_TEST"
116 #endif
117 (void) textdomain(TEXT_DOMAIN);
119 prog = prog_name(argv[0]);
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();
130 switch (c) {
131 case 'Q':
132 ckquit = 0;
133 break;
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);
143 break;
145 case 'd':
146 deflt = optarg;
147 break;
149 case 'p':
150 prompt = optarg;
151 break;
153 case 'e':
154 error = optarg;
155 break;
157 case 'h':
158 help = optarg;
159 break;
161 case 'k':
162 kpid = atoi(optarg);
163 break;
165 case 's':
166 signo = atoi(optarg);
167 break;
169 default:
170 usage();
174 if (signo) {
175 if (kpid == BADPID)
176 usage();
177 } else
178 signo = SIGTERM;
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);
189 if (optind != argc)
190 usage();
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);
202 if (deflt) {
203 len = strlen(deflt) + 1;
204 if (len < MAX_INPUT)
205 len = MAX_INPUT;
206 } else {
207 len = MAX_INPUT;
209 ynval = (char *)malloc(len);
210 if (!ynval) {
211 (void) fprintf(stderr,
212 gettext("Not enough memory\n"));
213 exit(1);
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);
224 (void) puts("q");
225 } else if (n == 0)
226 (void) fputs(ynval, stdout);
227 return (n);