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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23 /* All Rights Reserved */ 24 25 26 /* 27 * Copyright (c) 1997, by Sun Microsystems, Inc. 28 * All rights reserved. 29 */ 30 31 /*LINTLIBRARY*/ 32 33 #include <stdio.h> 34 #include <string.h> 35 #include <stdlib.h> 36 #include <limits.h> 37 #include <sys/types.h> 38 #include "libadm.h" 39 40 #define MSGSIZ 256 41 #define PROMPT10 "Enter an integer between %ld and %ld" 42 #define PROMPT "Enter a base %d integer between %ld and %ld" 43 #define MESG10 "Please enter an integer between %ld and %ld." 44 #define MESG "Please enter a base %d integer between %ld and %ld." 45 46 static void 47 setmsg(char *msg, long lower, long upper, int base) 48 { 49 if ((base == 10) || (base == 0)) 50 (void) sprintf(msg, MESG10, lower, upper); 51 else 52 (void) sprintf(msg, MESG, base, lower, upper); 53 } 54 55 void 56 ckrange_err(long lower, long upper, int base, char *error) 57 { 58 char defmesg[MSGSIZ]; 59 60 setmsg(defmesg, lower, upper, base); 61 puterror(stdout, defmesg, error); 62 } 63 64 void 65 ckrange_hlp(long lower, long upper, int base, char *help) 66 { 67 char defmesg[MSGSIZ]; 68 69 setmsg(defmesg, lower, upper, base); 70 puthelp(stdout, defmesg, help); 71 } 72 73 int 74 ckrange_val(long lower, long upper, int base, char *input) 75 { 76 char *ptr; 77 long value; 78 79 value = strtol(input, &ptr, base); 80 if ((*ptr != '\0') || (value < lower) || (value > upper)) 81 return (1); 82 return (0); 83 } 84 85 int 86 ckrange(long *rngval, long lower, long upper, short base, char *defstr, 87 char *error, char *help, char *prompt) 88 { 89 int valid, n; 90 long value; 91 char *ptr; 92 char input[MAX_INPUT]; 93 char defmesg[MSGSIZ]; 94 char defpmpt[128]; 95 char buffer[64]; 96 char *choices[2]; 97 98 if (lower >= upper) 99 return (2); 100 101 (void) sprintf(buffer, "%ld-%ld", lower, upper); 102 103 if (base == 0) 104 base = 10; 105 106 if (!prompt) { 107 if (base == 10) 108 (void) sprintf(defpmpt, PROMPT10, lower, upper); 109 else 110 (void) sprintf(defpmpt, PROMPT, base, lower, upper); 111 prompt = defpmpt; 112 } 113 114 setmsg(defmesg, lower, upper, base); 115 choices[0] = buffer; 116 choices[1] = NULL; 117 118 start: 119 putprmpt(stderr, prompt, choices, defstr); 120 if (getinput(input)) 121 return (1); 122 123 n = (int)strlen(input); 124 if (n == 0) { 125 if (defstr) { 126 *rngval = strtol(defstr, NULL, base); 127 return (0); 128 } 129 puterror(stderr, defmesg, error); 130 goto start; 131 } 132 if (strcmp(input, "?") == 0) { 133 puthelp(stderr, defmesg, help); 134 goto start; 135 } 136 if (ckquit && (strcmp(input, "q") == 0)) 137 return (3); 138 139 value = strtol(input, &ptr, base); 140 if (*ptr == '\0') 141 valid = ((value >= lower) && (value <= upper)); 142 else 143 valid = 0; 144 if (!valid) { 145 puterror(stderr, defmesg, error); 146 goto start; 147 } 148 *rngval = value; 149 return (0); 150 } 151