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