19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1991 Keith Muller. 39b50d902SRodney W. Grimes * Copyright (c) 1993 49b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 59b50d902SRodney W. Grimes * 69b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 79b50d902SRodney W. Grimes * Keith Muller of the University of California, San Diego. 89b50d902SRodney W. Grimes * 99b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 109b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 119b50d902SRodney W. Grimes * are met: 129b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 149b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 159b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 169b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 179b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 189b50d902SRodney W. Grimes * must display the following acknowledgement: 199b50d902SRodney W. Grimes * This product includes software developed by the University of 209b50d902SRodney W. Grimes * California, Berkeley and its contributors. 219b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 229b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 239b50d902SRodney W. Grimes * without specific prior written permission. 249b50d902SRodney W. Grimes * 259b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 269b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 279b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 289b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 299b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 309b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 319b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 329b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 339b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 349b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 359b50d902SRodney W. Grimes * SUCH DAMAGE. 369b50d902SRodney W. Grimes */ 379b50d902SRodney W. Grimes 38e0761ab1SMark Murray #if 0 399b50d902SRodney W. Grimes #ifndef lint 409b50d902SRodney W. Grimes static char sccsid[] = "@(#)egetopt.c 8.1 (Berkeley) 6/6/93"; 419b50d902SRodney W. Grimes #endif /* not lint */ 42e0761ab1SMark Murray #endif 43e0761ab1SMark Murray 44e0761ab1SMark Murray #include <sys/cdefs.h> 45e0761ab1SMark Murray __FBSDID("$FreeBSD$"); 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes #include <ctype.h> 489b50d902SRodney W. Grimes #include <stdio.h> 499b50d902SRodney W. Grimes #include <string.h> 509b50d902SRodney W. Grimes 51f319c4b9SEd Schouten #include "extern.h" 52f319c4b9SEd Schouten 539b50d902SRodney W. Grimes /* 549b50d902SRodney W. Grimes * egetopt: get option letter from argument vector (an extended 559b50d902SRodney W. Grimes * version of getopt). 569b50d902SRodney W. Grimes * 579b50d902SRodney W. Grimes * Non standard additions to the ostr specs are: 589b50d902SRodney W. Grimes * 1) '?': immediate value following arg is optional (no white space 599b50d902SRodney W. Grimes * between the arg and the value) 609b50d902SRodney W. Grimes * 2) '#': +/- followed by a number (with an optional sign but 619b50d902SRodney W. Grimes * no white space between the arg and the number). The - may be 629b50d902SRodney W. Grimes * combined with other options, but the + cannot. 639b50d902SRodney W. Grimes */ 649b50d902SRodney W. Grimes 659b50d902SRodney W. Grimes int eopterr = 1; /* if error message should be printed */ 669b50d902SRodney W. Grimes int eoptind = 1; /* index into parent argv vector */ 679b50d902SRodney W. Grimes int eoptopt; /* character checked for validity */ 689b50d902SRodney W. Grimes char *eoptarg; /* argument associated with option */ 699b50d902SRodney W. Grimes 709b50d902SRodney W. Grimes #define BADCH (int)'?' 71e0761ab1SMark Murray 72e0761ab1SMark Murray static char emsg[] = ""; 739b50d902SRodney W. Grimes 749b50d902SRodney W. Grimes int 75e0761ab1SMark Murray egetopt(int nargc, char * const *nargv, const char *ostr) 769b50d902SRodney W. Grimes { 77e0761ab1SMark Murray static char *place = emsg; /* option letter processing */ 78e0761ab1SMark Murray char *oli; /* option letter list index */ 799b50d902SRodney W. Grimes static int delim; /* which option delimeter */ 80e0761ab1SMark Murray char *p; 819b50d902SRodney W. Grimes static char savec = '\0'; 829b50d902SRodney W. Grimes 839b50d902SRodney W. Grimes if (savec != '\0') { 849b50d902SRodney W. Grimes *place = savec; 859b50d902SRodney W. Grimes savec = '\0'; 869b50d902SRodney W. Grimes } 879b50d902SRodney W. Grimes 889b50d902SRodney W. Grimes if (!*place) { 899b50d902SRodney W. Grimes /* 909b50d902SRodney W. Grimes * update scanning pointer 919b50d902SRodney W. Grimes */ 929b50d902SRodney W. Grimes if ((eoptind >= nargc) || 939b50d902SRodney W. Grimes ((*(place = nargv[eoptind]) != '-') && (*place != '+'))) { 94e0761ab1SMark Murray place = emsg; 95a228302fSPhilippe Charnier return (-1); 969b50d902SRodney W. Grimes } 979b50d902SRodney W. Grimes 989b50d902SRodney W. Grimes delim = (int)*place; 999b50d902SRodney W. Grimes if (place[1] && *++place == '-' && !place[1]) { 1009b50d902SRodney W. Grimes /* 1019b50d902SRodney W. Grimes * found "--" 1029b50d902SRodney W. Grimes */ 1039b50d902SRodney W. Grimes ++eoptind; 104e0761ab1SMark Murray place = emsg; 105a228302fSPhilippe Charnier return (-1); 1069b50d902SRodney W. Grimes } 1079b50d902SRodney W. Grimes } 1089b50d902SRodney W. Grimes 1099b50d902SRodney W. Grimes /* 1109b50d902SRodney W. Grimes * check option letter 1119b50d902SRodney W. Grimes */ 1129b50d902SRodney W. Grimes if ((eoptopt = (int)*place++) == (int)':' || (eoptopt == (int)'?') || 1139b50d902SRodney W. Grimes !(oli = strchr(ostr, eoptopt))) { 1149b50d902SRodney W. Grimes /* 1159b50d902SRodney W. Grimes * if the user didn't specify '-' as an option, 116a228302fSPhilippe Charnier * assume it means -1 when by itself. 1179b50d902SRodney W. Grimes */ 1189b50d902SRodney W. Grimes if ((eoptopt == (int)'-') && !*place) 119a228302fSPhilippe Charnier return (-1); 1209b50d902SRodney W. Grimes if (strchr(ostr, '#') && (isdigit(eoptopt) || 1219b50d902SRodney W. Grimes (((eoptopt == (int)'-') || (eoptopt == (int)'+')) && 1229b50d902SRodney W. Grimes isdigit(*place)))) { 1239b50d902SRodney W. Grimes /* 1249b50d902SRodney W. Grimes * # option: +/- with a number is ok 1259b50d902SRodney W. Grimes */ 1269b50d902SRodney W. Grimes for (p = place; *p != '\0'; ++p) { 1279b50d902SRodney W. Grimes if (!isdigit(*p)) 1289b50d902SRodney W. Grimes break; 1299b50d902SRodney W. Grimes } 1309b50d902SRodney W. Grimes eoptarg = place-1; 1319b50d902SRodney W. Grimes 1329b50d902SRodney W. Grimes if (*p == '\0') { 133e0761ab1SMark Murray place = emsg; 1349b50d902SRodney W. Grimes ++eoptind; 1359b50d902SRodney W. Grimes } else { 1369b50d902SRodney W. Grimes place = p; 1379b50d902SRodney W. Grimes savec = *p; 1389b50d902SRodney W. Grimes *place = '\0'; 1399b50d902SRodney W. Grimes } 1409b50d902SRodney W. Grimes return (delim); 1419b50d902SRodney W. Grimes } 1429b50d902SRodney W. Grimes 1439b50d902SRodney W. Grimes if (!*place) 1449b50d902SRodney W. Grimes ++eoptind; 1459b50d902SRodney W. Grimes if (eopterr) { 1469b50d902SRodney W. Grimes if (!(p = strrchr(*nargv, '/'))) 1479b50d902SRodney W. Grimes p = *nargv; 1489b50d902SRodney W. Grimes else 1499b50d902SRodney W. Grimes ++p; 1509b50d902SRodney W. Grimes (void)fprintf(stderr, "%s: illegal option -- %c\n", 1519b50d902SRodney W. Grimes p, eoptopt); 1529b50d902SRodney W. Grimes } 1539b50d902SRodney W. Grimes return (BADCH); 1549b50d902SRodney W. Grimes } 1559b50d902SRodney W. Grimes if (delim == (int)'+') { 1569b50d902SRodney W. Grimes /* 1579b50d902SRodney W. Grimes * '+' is only allowed with numbers 1589b50d902SRodney W. Grimes */ 1599b50d902SRodney W. Grimes if (!*place) 1609b50d902SRodney W. Grimes ++eoptind; 1619b50d902SRodney W. Grimes if (eopterr) { 1629b50d902SRodney W. Grimes if (!(p = strrchr(*nargv, '/'))) 1639b50d902SRodney W. Grimes p = *nargv; 1649b50d902SRodney W. Grimes else 1659b50d902SRodney W. Grimes ++p; 1669b50d902SRodney W. Grimes (void)fprintf(stderr, 1679b50d902SRodney W. Grimes "%s: illegal '+' delimiter with option -- %c\n", 1689b50d902SRodney W. Grimes p, eoptopt); 1699b50d902SRodney W. Grimes } 1709b50d902SRodney W. Grimes return (BADCH); 1719b50d902SRodney W. Grimes } 1729b50d902SRodney W. Grimes ++oli; 1739b50d902SRodney W. Grimes if ((*oli != ':') && (*oli != '?')) { 1749b50d902SRodney W. Grimes /* 1759b50d902SRodney W. Grimes * don't need argument 1769b50d902SRodney W. Grimes */ 1779b50d902SRodney W. Grimes eoptarg = NULL; 1789b50d902SRodney W. Grimes if (!*place) 1799b50d902SRodney W. Grimes ++eoptind; 1809b50d902SRodney W. Grimes return (eoptopt); 1819b50d902SRodney W. Grimes } 1829b50d902SRodney W. Grimes 1839b50d902SRodney W. Grimes if (*place) { 1849b50d902SRodney W. Grimes /* 1859b50d902SRodney W. Grimes * no white space 1869b50d902SRodney W. Grimes */ 1879b50d902SRodney W. Grimes eoptarg = place; 1889b50d902SRodney W. Grimes } else if (*oli == '?') { 1899b50d902SRodney W. Grimes /* 1909b50d902SRodney W. Grimes * no arg, but NOT required 1919b50d902SRodney W. Grimes */ 1929b50d902SRodney W. Grimes eoptarg = NULL; 1939b50d902SRodney W. Grimes } else if (nargc <= ++eoptind) { 1949b50d902SRodney W. Grimes /* 1959b50d902SRodney W. Grimes * no arg, but IS required 1969b50d902SRodney W. Grimes */ 197e0761ab1SMark Murray place = emsg; 1989b50d902SRodney W. Grimes if (eopterr) { 1999b50d902SRodney W. Grimes if (!(p = strrchr(*nargv, '/'))) 2009b50d902SRodney W. Grimes p = *nargv; 2019b50d902SRodney W. Grimes else 2029b50d902SRodney W. Grimes ++p; 2039b50d902SRodney W. Grimes (void)fprintf(stderr, 2049b50d902SRodney W. Grimes "%s: option requires an argument -- %c\n", p, 2059b50d902SRodney W. Grimes eoptopt); 2069b50d902SRodney W. Grimes } 2079b50d902SRodney W. Grimes return (BADCH); 2089b50d902SRodney W. Grimes } else { 2099b50d902SRodney W. Grimes /* 2109b50d902SRodney W. Grimes * arg has white space 2119b50d902SRodney W. Grimes */ 2129b50d902SRodney W. Grimes eoptarg = nargv[eoptind]; 2139b50d902SRodney W. Grimes } 214e0761ab1SMark Murray place = emsg; 2159b50d902SRodney W. Grimes ++eoptind; 2169b50d902SRodney W. Grimes return (eoptopt); 2179b50d902SRodney W. Grimes } 218