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