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 #include <stdio.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <stdlib.h>
34 #include <locale.h>
35 #include <libintl.h>
36 #include <limits.h>
37 #include "usage.h"
38 #include "libadm.h"
39
40 #define BADPID (-2)
41
42 static char *prog;
43 static char *deflt = NULL, *prompt = NULL, *error = NULL, *help = NULL;
44 static int kpid = BADPID;
45 static int signo;
46
47 static char **keyword;
48 static int nkeyword = 0;
49
50 static void
usage(void)51 usage(void)
52 {
53 (void) fprintf(stderr,
54 gettext("usage: %s [options] keyword [...]\n"), prog);
55 (void) fprintf(stderr, gettext(OPTMESG));
56 (void) fprintf(stderr, gettext(STDOPTS));
57 exit(1);
58 }
59
60 /*
61 * Given argv[0], return a pointer to the basename of the program.
62 */
63 static char *
prog_name(char * arg0)64 prog_name(char *arg0)
65 {
66 char *str;
67
68 /* first strip trailing '/' characters (exec() allows these!) */
69 str = arg0 + strlen(arg0);
70 while (str > arg0 && *--str == '/')
71 *str = '\0';
72 if ((str = strrchr(arg0, '/')) != NULL)
73 return (str + 1);
74 return (arg0);
75 }
76
77 int
main(int argc,char ** argv)78 main(int argc, char **argv)
79 {
80 int c, n;
81 int i;
82 char *strval;
83 size_t len;
84
85 (void) setlocale(LC_ALL, "");
86
87 #if !defined(TEXT_DOMAIN)
88 #define TEXT_DOMAIN "SYS_TEST"
89 #endif
90 (void) textdomain(TEXT_DOMAIN);
91
92 prog = prog_name(argv[0]);
93
94
95 while ((c = getopt(argc, argv, "d:p:e:h:k:s:QW:?")) != EOF) {
96 switch (c) {
97 case 'Q':
98 ckquit = 0;
99 break;
100
101 case 'W':
102 ckwidth = atoi(optarg);
103 if (ckwidth < 0) {
104 (void) fprintf(stderr,
105 gettext("%s: ERROR: negative display width specified\n"),
106 prog);
107 exit(1);
108 }
109 break;
110
111 case 'd':
112 deflt = optarg;
113 break;
114
115 case 'p':
116 prompt = optarg;
117 break;
118
119 case 'e':
120 error = optarg;
121 break;
122
123 case 'h':
124 help = optarg;
125 break;
126
127 case 'k':
128 kpid = atoi(optarg);
129 break;
130
131 case 's':
132 signo = atoi(optarg);
133 break;
134
135 default:
136 usage();
137 }
138 }
139
140 if (signo) {
141 if (kpid == BADPID)
142 usage();
143 } else
144 signo = SIGTERM;
145
146 if (optind >= argc)
147 usage(); /* must be at least one keyword */
148
149 nkeyword = argc - optind;
150 keyword = (char **)malloc(sizeof (char *) * (nkeyword + 1));
151 if (!keyword) {
152 (void) fprintf(stderr,
153 gettext("Not enough memory\n"));
154 exit(1);
155 }
156 for (i = 0; i < nkeyword; i++)
157 keyword[i] = argv[optind++];
158 keyword[nkeyword] = NULL;
159
160 if (deflt) {
161 len = strlen(deflt) + 1;
162 if (len < MAX_INPUT)
163 len = MAX_INPUT;
164 } else {
165 len = MAX_INPUT;
166 }
167 strval = (char *)malloc(len);
168 if (!strval) {
169 (void) fprintf(stderr,
170 gettext("Not enough memory\n"));
171 exit(1);
172 }
173 n = ckkeywd(strval, keyword, deflt, error, help, prompt);
174 if (n == 3) {
175 if (kpid > -2)
176 (void) kill(kpid, signo);
177 (void) puts("q");
178 } else if (n == 0)
179 (void) fputs(strval, stdout);
180 return (n);
181 }
182