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 "usage.h" 37 #include "libadm.h" 38 39 #define BADPID (-2) 40 41 static char *prog; 42 static char *deflt, *prompt, *error, *help; 43 static int kpid = BADPID; 44 static int signo; 45 static short base; 46 47 static const char vusage[] = "b"; 48 static const char husage[] = "bWh"; 49 static const char eusage[] = "bWe"; 50 51 static void 52 usage(void) 53 { 54 switch (*prog) { 55 default: 56 (void) fprintf(stderr, 57 gettext("usage: %s [options] [-b base]\n"), prog); 58 (void) fprintf(stderr, gettext(OPTMESG)); 59 (void) fprintf(stderr, gettext(STDOPTS)); 60 break; 61 62 case 'v': 63 (void) fprintf(stderr, 64 gettext("usage: %s [-b base] input\n"), prog); 65 break; 66 67 case 'h': 68 (void) fprintf(stderr, 69 gettext("usage: %s [options] [-b base]\n"), prog); 70 (void) fprintf(stderr, gettext(OPTMESG)); 71 (void) fprintf(stderr, 72 gettext("\t-W width\n\t-h help\n")); 73 break; 74 75 case 'e': 76 (void) fprintf(stderr, 77 gettext("usage: %s [options] [-b base]\n"), prog); 78 (void) fprintf(stderr, gettext(OPTMESG)); 79 (void) fprintf(stderr, 80 gettext("\t-W width\n\t-e error\n")); 81 break; 82 } 83 exit(1); 84 } 85 86 /* 87 * Given argv[0], return a pointer to the basename of the program. 88 */ 89 static char * 90 prog_name(char *arg0) 91 { 92 char *str; 93 94 /* first strip trailing '/' characters (exec() allows these!) */ 95 str = arg0 + strlen(arg0); 96 while (str > arg0 && *--str == '/') 97 *str = '\0'; 98 if ((str = strrchr(arg0, '/')) != NULL) 99 return (str + 1); 100 return (arg0); 101 } 102 103 int 104 main(int argc, char **argv) 105 { 106 int c, n; 107 long intval; 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, "b:d:p:e:h:k:s:QW:?")) != EOF) { 119 /* check for invalid option */ 120 if ((*prog == 'v') && !strchr(vusage, c)) 121 usage(); /* no valid options */ 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 'b': 143 base = atoi(optarg); 144 if ((base < 2) || (base > 36)) { 145 (void) fprintf(stderr, 146 gettext("%s: ERROR: base must be between 2 and 36\n"), 147 prog); 148 exit(1); 149 } 150 break; 151 152 case 'd': 153 deflt = optarg; 154 break; 155 156 case 'p': 157 prompt = optarg; 158 break; 159 160 case 'e': 161 error = optarg; 162 break; 163 164 case 'h': 165 help = optarg; 166 break; 167 168 case 'k': 169 kpid = atoi(optarg); 170 break; 171 172 case 's': 173 signo = atoi(optarg); 174 break; 175 176 default: 177 usage(); 178 } 179 } 180 181 if (signo) { 182 if (kpid == BADPID) 183 usage(); 184 } else 185 signo = SIGTERM; 186 187 if (*prog == 'v') { 188 if (argc != (optind + 1)) 189 usage(); 190 exit(ckint_val(argv[optind], base)); 191 } 192 193 if (optind != argc) 194 usage(); 195 196 if (*prog == 'e') { 197 ckindent = 0; 198 ckint_err(base, error); 199 exit(0); 200 } else if (*prog == 'h') { 201 ckindent = 0; 202 ckint_hlp(base, help); 203 exit(0); 204 } 205 206 n = ckint(&intval, base, deflt, error, help, prompt); 207 if (n == 3) { 208 if (kpid > -2) 209 (void) kill(kpid, signo); 210 (void) puts("q"); 211 } else if (n == 0) 212 (void) printf("%ld", intval); 213 return (n); 214 } 215