xref: /freebsd/usr.bin/pr/egetopt.c (revision a228302fcae9adc0c0daf987422f35e664f0796b)
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 <stdlib.h>
509b50d902SRodney W. Grimes #include <string.h>
519b50d902SRodney W. Grimes 
529b50d902SRodney W. Grimes #include "extern.h"
539b50d902SRodney W. Grimes 
549b50d902SRodney W. Grimes /*
559b50d902SRodney W. Grimes  * egetopt:	get option letter from argument vector (an extended
569b50d902SRodney W. Grimes  *		version of getopt).
579b50d902SRodney W. Grimes  *
589b50d902SRodney W. Grimes  * Non standard additions to the ostr specs are:
599b50d902SRodney W. Grimes  * 1) '?': immediate value following arg is optional (no white space
609b50d902SRodney W. Grimes  *    between the arg and the value)
619b50d902SRodney W. Grimes  * 2) '#': +/- followed by a number (with an optional sign but
629b50d902SRodney W. Grimes  *    no white space between the arg and the number). The - may be
639b50d902SRodney W. Grimes  *    combined with other options, but the + cannot.
649b50d902SRodney W. Grimes  */
659b50d902SRodney W. Grimes 
669b50d902SRodney W. Grimes int	eopterr = 1;		/* if error message should be printed */
679b50d902SRodney W. Grimes int	eoptind = 1;		/* index into parent argv vector */
689b50d902SRodney W. Grimes int	eoptopt;		/* character checked for validity */
699b50d902SRodney W. Grimes char	*eoptarg;		/* argument associated with option */
709b50d902SRodney W. Grimes 
719b50d902SRodney W. Grimes #define	BADCH	(int)'?'
72e0761ab1SMark Murray 
73e0761ab1SMark Murray static char	emsg[] = "";
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes int
76e0761ab1SMark Murray egetopt(int nargc, char * const *nargv, const char *ostr)
779b50d902SRodney W. Grimes {
78e0761ab1SMark Murray 	static char *place = emsg;	/* option letter processing */
79e0761ab1SMark Murray 	char *oli;			/* option letter list index */
809b50d902SRodney W. Grimes 	static int delim;		/* which option delimeter */
81e0761ab1SMark Murray 	char *p;
829b50d902SRodney W. Grimes 	static char savec = '\0';
839b50d902SRodney W. Grimes 
849b50d902SRodney W. Grimes 	if (savec != '\0') {
859b50d902SRodney W. Grimes 		*place = savec;
869b50d902SRodney W. Grimes 		savec = '\0';
879b50d902SRodney W. Grimes 	}
889b50d902SRodney W. Grimes 
899b50d902SRodney W. Grimes 	if (!*place) {
909b50d902SRodney W. Grimes 		/*
919b50d902SRodney W. Grimes 		 * update scanning pointer
929b50d902SRodney W. Grimes 		 */
939b50d902SRodney W. Grimes 		if ((eoptind >= nargc) ||
949b50d902SRodney W. Grimes 		    ((*(place = nargv[eoptind]) != '-') && (*place != '+'))) {
95e0761ab1SMark Murray 			place = emsg;
96a228302fSPhilippe Charnier 			return (-1);
979b50d902SRodney W. Grimes 		}
989b50d902SRodney W. Grimes 
999b50d902SRodney W. Grimes 		delim = (int)*place;
1009b50d902SRodney W. Grimes 		if (place[1] && *++place == '-' && !place[1]) {
1019b50d902SRodney W. Grimes 			/*
1029b50d902SRodney W. Grimes 			 * found "--"
1039b50d902SRodney W. Grimes 			 */
1049b50d902SRodney W. Grimes 			++eoptind;
105e0761ab1SMark Murray 			place = emsg;
106a228302fSPhilippe Charnier 			return (-1);
1079b50d902SRodney W. Grimes 		}
1089b50d902SRodney W. Grimes 	}
1099b50d902SRodney W. Grimes 
1109b50d902SRodney W. Grimes 	/*
1119b50d902SRodney W. Grimes 	 * check option letter
1129b50d902SRodney W. Grimes 	 */
1139b50d902SRodney W. Grimes 	if ((eoptopt = (int)*place++) == (int)':' || (eoptopt == (int)'?') ||
1149b50d902SRodney W. Grimes 	    !(oli = strchr(ostr, eoptopt))) {
1159b50d902SRodney W. Grimes 		/*
1169b50d902SRodney W. Grimes 		 * if the user didn't specify '-' as an option,
117a228302fSPhilippe Charnier 		 * assume it means -1 when by itself.
1189b50d902SRodney W. Grimes 		 */
1199b50d902SRodney W. Grimes 		if ((eoptopt == (int)'-') && !*place)
120a228302fSPhilippe Charnier 			return (-1);
1219b50d902SRodney W. Grimes 		if (strchr(ostr, '#') && (isdigit(eoptopt) ||
1229b50d902SRodney W. Grimes 		    (((eoptopt == (int)'-') || (eoptopt == (int)'+')) &&
1239b50d902SRodney W. Grimes 		      isdigit(*place)))) {
1249b50d902SRodney W. Grimes 			/*
1259b50d902SRodney W. Grimes 			 * # option: +/- with a number is ok
1269b50d902SRodney W. Grimes 			 */
1279b50d902SRodney W. Grimes 			for (p = place; *p != '\0'; ++p) {
1289b50d902SRodney W. Grimes 				if (!isdigit(*p))
1299b50d902SRodney W. Grimes 					break;
1309b50d902SRodney W. Grimes 			}
1319b50d902SRodney W. Grimes 			eoptarg = place-1;
1329b50d902SRodney W. Grimes 
1339b50d902SRodney W. Grimes 			if (*p == '\0') {
134e0761ab1SMark Murray 				place = emsg;
1359b50d902SRodney W. Grimes 				++eoptind;
1369b50d902SRodney W. Grimes 			} else {
1379b50d902SRodney W. Grimes 				place = p;
1389b50d902SRodney W. Grimes 				savec = *p;
1399b50d902SRodney W. Grimes 				*place = '\0';
1409b50d902SRodney W. Grimes 			}
1419b50d902SRodney W. Grimes 			return (delim);
1429b50d902SRodney W. Grimes 		}
1439b50d902SRodney W. Grimes 
1449b50d902SRodney W. Grimes 		if (!*place)
1459b50d902SRodney W. Grimes 			++eoptind;
1469b50d902SRodney W. Grimes 		if (eopterr) {
1479b50d902SRodney W. Grimes 			if (!(p = strrchr(*nargv, '/')))
1489b50d902SRodney W. Grimes 				p = *nargv;
1499b50d902SRodney W. Grimes 			else
1509b50d902SRodney W. Grimes 				++p;
1519b50d902SRodney W. Grimes 			(void)fprintf(stderr, "%s: illegal option -- %c\n",
1529b50d902SRodney W. Grimes 			    p, eoptopt);
1539b50d902SRodney W. Grimes 		}
1549b50d902SRodney W. Grimes 		return (BADCH);
1559b50d902SRodney W. Grimes 	}
1569b50d902SRodney W. Grimes 	if (delim == (int)'+') {
1579b50d902SRodney W. Grimes 		/*
1589b50d902SRodney W. Grimes 		 * '+' is only allowed with numbers
1599b50d902SRodney W. Grimes 		 */
1609b50d902SRodney W. Grimes 		if (!*place)
1619b50d902SRodney W. Grimes 			++eoptind;
1629b50d902SRodney W. Grimes 		if (eopterr) {
1639b50d902SRodney W. Grimes 			if (!(p = strrchr(*nargv, '/')))
1649b50d902SRodney W. Grimes 				p = *nargv;
1659b50d902SRodney W. Grimes 			else
1669b50d902SRodney W. Grimes 				++p;
1679b50d902SRodney W. Grimes 			(void)fprintf(stderr,
1689b50d902SRodney W. Grimes 				"%s: illegal '+' delimiter with option -- %c\n",
1699b50d902SRodney W. Grimes 				p, eoptopt);
1709b50d902SRodney W. Grimes 		}
1719b50d902SRodney W. Grimes 		return (BADCH);
1729b50d902SRodney W. Grimes 	}
1739b50d902SRodney W. Grimes 	++oli;
1749b50d902SRodney W. Grimes 	if ((*oli != ':') && (*oli != '?')) {
1759b50d902SRodney W. Grimes 		/*
1769b50d902SRodney W. Grimes 		 * don't need argument
1779b50d902SRodney W. Grimes 		 */
1789b50d902SRodney W. Grimes 		eoptarg = NULL;
1799b50d902SRodney W. Grimes 		if (!*place)
1809b50d902SRodney W. Grimes 			++eoptind;
1819b50d902SRodney W. Grimes 		return (eoptopt);
1829b50d902SRodney W. Grimes 	}
1839b50d902SRodney W. Grimes 
1849b50d902SRodney W. Grimes 	if (*place) {
1859b50d902SRodney W. Grimes 		/*
1869b50d902SRodney W. Grimes 		 * no white space
1879b50d902SRodney W. Grimes 		 */
1889b50d902SRodney W. Grimes 		eoptarg = place;
1899b50d902SRodney W. Grimes 	} else if (*oli == '?') {
1909b50d902SRodney W. Grimes 		/*
1919b50d902SRodney W. Grimes 		 * no arg, but NOT required
1929b50d902SRodney W. Grimes 		 */
1939b50d902SRodney W. Grimes 		eoptarg = NULL;
1949b50d902SRodney W. Grimes 	} else if (nargc <= ++eoptind) {
1959b50d902SRodney W. Grimes 		/*
1969b50d902SRodney W. Grimes 		 * no arg, but IS required
1979b50d902SRodney W. Grimes 		 */
198e0761ab1SMark Murray 		place = emsg;
1999b50d902SRodney W. Grimes 		if (eopterr) {
2009b50d902SRodney W. Grimes 			if (!(p = strrchr(*nargv, '/')))
2019b50d902SRodney W. Grimes 				p = *nargv;
2029b50d902SRodney W. Grimes 			else
2039b50d902SRodney W. Grimes 				++p;
2049b50d902SRodney W. Grimes 			(void)fprintf(stderr,
2059b50d902SRodney W. Grimes 			    "%s: option requires an argument -- %c\n", p,
2069b50d902SRodney W. Grimes 			    eoptopt);
2079b50d902SRodney W. Grimes 		}
2089b50d902SRodney W. Grimes 		return (BADCH);
2099b50d902SRodney W. Grimes 	} else {
2109b50d902SRodney W. Grimes 		/*
2119b50d902SRodney W. Grimes 		 * arg has white space
2129b50d902SRodney W. Grimes 		 */
2139b50d902SRodney W. Grimes 		eoptarg = nargv[eoptind];
2149b50d902SRodney W. Grimes 	}
215e0761ab1SMark Murray 	place = emsg;
2169b50d902SRodney W. Grimes 	++eoptind;
2179b50d902SRodney W. Grimes 	return (eoptopt);
2189b50d902SRodney W. Grimes }
219