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