1 1.9 joerg /* $NetBSD: egetopt.c,v 1.9 2011/09/06 18:26:06 joerg Exp $ */ 2 1.2 tls 3 1.1 cgd /*- 4 1.7 agc * Copyright (c) 1991 Keith Muller. 5 1.1 cgd * Copyright (c) 1993 6 1.1 cgd * The Regents of the University of California. All rights reserved. 7 1.1 cgd * 8 1.1 cgd * This code is derived from software contributed to Berkeley by 9 1.1 cgd * Keith Muller of the University of California, San Diego. 10 1.1 cgd * 11 1.1 cgd * Redistribution and use in source and binary forms, with or without 12 1.1 cgd * modification, are permitted provided that the following conditions 13 1.1 cgd * are met: 14 1.1 cgd * 1. Redistributions of source code must retain the above copyright 15 1.1 cgd * notice, this list of conditions and the following disclaimer. 16 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 cgd * notice, this list of conditions and the following disclaimer in the 18 1.1 cgd * documentation and/or other materials provided with the distribution. 19 1.6 agc * 3. Neither the name of the University nor the names of its contributors 20 1.6 agc * may be used to endorse or promote products derived from this software 21 1.6 agc * without specific prior written permission. 22 1.6 agc * 23 1.6 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 1.6 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 1.6 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 1.6 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 1.6 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 1.6 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 1.6 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 1.6 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 1.6 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 1.6 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 1.6 agc * SUCH DAMAGE. 34 1.6 agc */ 35 1.6 agc 36 1.3 lukem #include <sys/cdefs.h> 37 1.1 cgd #ifndef lint 38 1.3 lukem #if 0 39 1.3 lukem from: static char sccsid[] = "@(#)egetopt.c 8.1 (Berkeley) 6/6/93"; 40 1.3 lukem #else 41 1.9 joerg __RCSID("$NetBSD: egetopt.c,v 1.9 2011/09/06 18:26:06 joerg Exp $"); 42 1.3 lukem #endif 43 1.1 cgd #endif /* not lint */ 44 1.1 cgd 45 1.1 cgd #include <ctype.h> 46 1.1 cgd #include <stdio.h> 47 1.1 cgd #include <stdlib.h> 48 1.1 cgd #include <string.h> 49 1.1 cgd 50 1.1 cgd #include "extern.h" 51 1.1 cgd 52 1.1 cgd /* 53 1.1 cgd * egetopt: get option letter from argument vector (an extended 54 1.1 cgd * version of getopt). 55 1.1 cgd * 56 1.1 cgd * Non standard additions to the ostr specs are: 57 1.1 cgd * 1) '?': immediate value following arg is optional (no white space 58 1.1 cgd * between the arg and the value) 59 1.1 cgd * 2) '#': +/- followed by a number (with an optional sign but 60 1.1 cgd * no white space between the arg and the number). The - may be 61 1.1 cgd * combined with other options, but the + cannot. 62 1.1 cgd */ 63 1.1 cgd 64 1.1 cgd int eopterr = 1; /* if error message should be printed */ 65 1.1 cgd int eoptind = 1; /* index into parent argv vector */ 66 1.1 cgd int eoptopt; /* character checked for validity */ 67 1.1 cgd char *eoptarg; /* argument associated with option */ 68 1.1 cgd 69 1.1 cgd #define BADCH (int)'?' 70 1.9 joerg static char EMSG[1] = { '\0' }; 71 1.1 cgd 72 1.1 cgd int 73 1.9 joerg egetopt(int nargc, char * const *nargv, const char *ostr) 74 1.1 cgd { 75 1.1 cgd static char *place = EMSG; /* option letter processing */ 76 1.3 lukem char *oli; /* option letter list index */ 77 1.5 wiz static int delim; /* which option delimiter */ 78 1.3 lukem char *p; 79 1.1 cgd static char savec = '\0'; 80 1.1 cgd 81 1.1 cgd if (savec != '\0') { 82 1.1 cgd *place = savec; 83 1.1 cgd savec = '\0'; 84 1.1 cgd } 85 1.1 cgd 86 1.1 cgd if (!*place) { 87 1.1 cgd /* 88 1.1 cgd * update scanning pointer 89 1.1 cgd */ 90 1.1 cgd if ((eoptind >= nargc) || 91 1.1 cgd ((*(place = nargv[eoptind]) != '-') && (*place != '+'))) { 92 1.1 cgd place = EMSG; 93 1.3 lukem return (-1); 94 1.1 cgd } 95 1.1 cgd 96 1.1 cgd delim = (int)*place; 97 1.1 cgd if (place[1] && *++place == '-' && !place[1]) { 98 1.1 cgd /* 99 1.1 cgd * found "--" 100 1.1 cgd */ 101 1.1 cgd ++eoptind; 102 1.1 cgd place = EMSG; 103 1.3 lukem return (-1); 104 1.1 cgd } 105 1.1 cgd } 106 1.1 cgd 107 1.1 cgd /* 108 1.1 cgd * check option letter 109 1.1 cgd */ 110 1.1 cgd if ((eoptopt = (int)*place++) == (int)':' || (eoptopt == (int)'?') || 111 1.1 cgd !(oli = strchr(ostr, eoptopt))) { 112 1.1 cgd /* 113 1.1 cgd * if the user didn't specify '-' as an option, 114 1.3 lukem * assume it means -1 when by itself. 115 1.1 cgd */ 116 1.1 cgd if ((eoptopt == (int)'-') && !*place) 117 1.3 lukem return (-1); 118 1.1 cgd if (strchr(ostr, '#') && (isdigit(eoptopt) || 119 1.1 cgd (((eoptopt == (int)'-') || (eoptopt == (int)'+')) && 120 1.4 christos isdigit((unsigned char)*place)))) { 121 1.1 cgd /* 122 1.1 cgd * # option: +/- with a number is ok 123 1.1 cgd */ 124 1.1 cgd for (p = place; *p != '\0'; ++p) { 125 1.4 christos if (!isdigit((unsigned char)*p)) 126 1.1 cgd break; 127 1.1 cgd } 128 1.1 cgd eoptarg = place-1; 129 1.1 cgd 130 1.1 cgd if (*p == '\0') { 131 1.1 cgd place = EMSG; 132 1.1 cgd ++eoptind; 133 1.1 cgd } else { 134 1.1 cgd place = p; 135 1.1 cgd savec = *p; 136 1.1 cgd *place = '\0'; 137 1.1 cgd } 138 1.1 cgd return (delim); 139 1.1 cgd } 140 1.1 cgd 141 1.1 cgd if (!*place) 142 1.1 cgd ++eoptind; 143 1.1 cgd if (eopterr) { 144 1.1 cgd if (!(p = strrchr(*nargv, '/'))) 145 1.1 cgd p = *nargv; 146 1.1 cgd else 147 1.1 cgd ++p; 148 1.1 cgd (void)fprintf(stderr, "%s: illegal option -- %c\n", 149 1.1 cgd p, eoptopt); 150 1.1 cgd } 151 1.1 cgd return (BADCH); 152 1.1 cgd } 153 1.1 cgd if (delim == (int)'+') { 154 1.1 cgd /* 155 1.1 cgd * '+' is only allowed with numbers 156 1.1 cgd */ 157 1.1 cgd if (!*place) 158 1.1 cgd ++eoptind; 159 1.1 cgd if (eopterr) { 160 1.1 cgd if (!(p = strrchr(*nargv, '/'))) 161 1.1 cgd p = *nargv; 162 1.1 cgd else 163 1.1 cgd ++p; 164 1.1 cgd (void)fprintf(stderr, 165 1.1 cgd "%s: illegal '+' delimiter with option -- %c\n", 166 1.1 cgd p, eoptopt); 167 1.1 cgd } 168 1.1 cgd return (BADCH); 169 1.1 cgd } 170 1.1 cgd ++oli; 171 1.1 cgd if ((*oli != ':') && (*oli != '?')) { 172 1.1 cgd /* 173 1.1 cgd * don't need argument 174 1.1 cgd */ 175 1.1 cgd eoptarg = NULL; 176 1.1 cgd if (!*place) 177 1.1 cgd ++eoptind; 178 1.1 cgd return (eoptopt); 179 1.1 cgd } 180 1.1 cgd 181 1.1 cgd if (*place) { 182 1.1 cgd /* 183 1.1 cgd * no white space 184 1.1 cgd */ 185 1.1 cgd eoptarg = place; 186 1.1 cgd } else if (*oli == '?') { 187 1.1 cgd /* 188 1.1 cgd * no arg, but NOT required 189 1.1 cgd */ 190 1.1 cgd eoptarg = NULL; 191 1.1 cgd } else if (nargc <= ++eoptind) { 192 1.1 cgd /* 193 1.1 cgd * no arg, but IS required 194 1.1 cgd */ 195 1.1 cgd place = EMSG; 196 1.1 cgd if (eopterr) { 197 1.1 cgd if (!(p = strrchr(*nargv, '/'))) 198 1.1 cgd p = *nargv; 199 1.1 cgd else 200 1.1 cgd ++p; 201 1.1 cgd (void)fprintf(stderr, 202 1.1 cgd "%s: option requires an argument -- %c\n", p, 203 1.1 cgd eoptopt); 204 1.1 cgd } 205 1.1 cgd return (BADCH); 206 1.1 cgd } else { 207 1.1 cgd /* 208 1.1 cgd * arg has white space 209 1.1 cgd */ 210 1.1 cgd eoptarg = nargv[eoptind]; 211 1.1 cgd } 212 1.1 cgd place = EMSG; 213 1.1 cgd ++eoptind; 214 1.1 cgd return (eoptopt); 215 1.1 cgd } 216