xref: /titanic_50/usr/src/cmd/valtools/ckkeywd.c (revision 4445fffbbb1ea25fd0e9ea68b9380dd7a6709025)
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 = NULL, *prompt = NULL, *error = NULL, *help = NULL;
47 static int	kpid = BADPID;
48 static int	signo;
49 
50 static char	**keyword;
51 static int	nkeyword = 0;
52 
53 static void
54 usage(void)
55 {
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);
61 }
62 
63 /*
64  * Given argv[0], return a pointer to the basename of the program.
65  */
66 static char *
67 prog_name(char *arg0)
68 {
69 	char *str;
70 
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);
78 }
79 
80 int
81 main(int argc, char **argv)
82 {
83 	int	c, n;
84 	int	i;
85 	char	*strval;
86 	size_t	len;
87 
88 	(void) setlocale(LC_ALL, "");
89 
90 #if	!defined(TEXT_DOMAIN)
91 #define	TEXT_DOMAIN	"SYS_TEST"
92 #endif
93 	(void) textdomain(TEXT_DOMAIN);
94 
95 	prog = prog_name(argv[0]);
96 
97 
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;
103 
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);
111 			}
112 			break;
113 
114 		case 'd':
115 			deflt = optarg;
116 			break;
117 
118 		case 'p':
119 			prompt = optarg;
120 			break;
121 
122 		case 'e':
123 			error = optarg;
124 			break;
125 
126 		case 'h':
127 			help = optarg;
128 			break;
129 
130 		case 'k':
131 			kpid = atoi(optarg);
132 			break;
133 
134 		case 's':
135 			signo = atoi(optarg);
136 			break;
137 
138 		default:
139 			usage();
140 		}
141 	}
142 
143 	if (signo) {
144 		if (kpid == BADPID)
145 			usage();
146 	} else
147 		signo = SIGTERM;
148 
149 	if (optind >= argc)
150 		usage(); /* must be at least one keyword */
151 
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);
158 	}
159 	for (i = 0; i < nkeyword; i++)
160 		keyword[i] = argv[optind++];
161 	keyword[nkeyword] = NULL;
162 
163 	if (deflt) {
164 		len = strlen(deflt) + 1;
165 		if (len < MAX_INPUT)
166 			len = MAX_INPUT;
167 	} else {
168 		len = MAX_INPUT;
169 	}
170 	strval = (char *)malloc(len);
171 	if (!strval) {
172 		(void) fprintf(stderr,
173 			gettext("Not enough memory\n"));
174 		exit(1);
175 	}
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);
184 }
185