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