8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / valtools / ckkeywd.c
blob6b0e670d585d718c438cdc0f4af7d942801a3b81
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 = NULL, *prompt = NULL, *error = NULL, *help = NULL;
47 static int kpid = BADPID;
48 static int signo;
50 static char **keyword;
51 static int nkeyword = 0;
53 static void
54 usage(void)
56 (void) fprintf(stderr,
57 gettext("usage: %s [options] keyword [...]\n"), prog);
58 (void) fprintf(stderr, gettext(OPTMESG));
59 (void) fprintf(stderr, gettext(STDOPTS));
60 exit(1);
64 * Given argv[0], return a pointer to the basename of the program.
66 static char *
67 prog_name(char *arg0)
69 char *str;
71 /* first strip trailing '/' characters (exec() allows these!) */
72 str = arg0 + strlen(arg0);
73 while (str > arg0 && *--str == '/')
74 *str = '\0';
75 if ((str = strrchr(arg0, '/')) != NULL)
76 return (str + 1);
77 return (arg0);
80 int
81 main(int argc, char **argv)
83 int c, n;
84 int i;
85 char *strval;
86 size_t len;
88 (void) setlocale(LC_ALL, "");
90 #if !defined(TEXT_DOMAIN)
91 #define TEXT_DOMAIN "SYS_TEST"
92 #endif
93 (void) textdomain(TEXT_DOMAIN);
95 prog = prog_name(argv[0]);
98 while ((c = getopt(argc, argv, "d:p:e:h:k:s:QW:?")) != EOF) {
99 switch (c) {
100 case 'Q':
101 ckquit = 0;
102 break;
104 case 'W':
105 ckwidth = atoi(optarg);
106 if (ckwidth < 0) {
107 (void) fprintf(stderr,
108 gettext("%s: ERROR: negative display width specified\n"),
109 prog);
110 exit(1);
112 break;
114 case 'd':
115 deflt = optarg;
116 break;
118 case 'p':
119 prompt = optarg;
120 break;
122 case 'e':
123 error = optarg;
124 break;
126 case 'h':
127 help = optarg;
128 break;
130 case 'k':
131 kpid = atoi(optarg);
132 break;
134 case 's':
135 signo = atoi(optarg);
136 break;
138 default:
139 usage();
143 if (signo) {
144 if (kpid == BADPID)
145 usage();
146 } else
147 signo = SIGTERM;
149 if (optind >= argc)
150 usage(); /* must be at least one keyword */
152 nkeyword = argc - optind;
153 keyword = (char **)malloc(sizeof (char *) * (nkeyword + 1));
154 if (!keyword) {
155 (void) fprintf(stderr,
156 gettext("Not enough memory\n"));
157 exit(1);
159 for (i = 0; i < nkeyword; i++)
160 keyword[i] = argv[optind++];
161 keyword[nkeyword] = NULL;
163 if (deflt) {
164 len = strlen(deflt) + 1;
165 if (len < MAX_INPUT)
166 len = MAX_INPUT;
167 } else {
168 len = MAX_INPUT;
170 strval = (char *)malloc(len);
171 if (!strval) {
172 (void) fprintf(stderr,
173 gettext("Not enough memory\n"));
174 exit(1);
176 n = ckkeywd(strval, keyword, deflt, error, help, prompt);
177 if (n == 3) {
178 if (kpid > -2)
179 (void) kill(kpid, signo);
180 (void) puts("q");
181 } else if (n == 0)
182 (void) fputs(strval, stdout);
183 return (n);