Home | History | Annotate | Line # | Download | only in pr
egetopt.c revision 1.6
      1  1.6       agc /*	$NetBSD: egetopt.c,v 1.6 2003/08/07 11:15:29 agc Exp $	*/
      2  1.2       tls 
      3  1.1       cgd /*-
      4  1.1       cgd  * Copyright (c) 1993
      5  1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * This code is derived from software contributed to Berkeley by
      8  1.1       cgd  * Keith Muller of the University of California, San Diego.
      9  1.1       cgd  *
     10  1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1       cgd  * modification, are permitted provided that the following conditions
     12  1.1       cgd  * are met:
     13  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.6       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.6       agc  *    may be used to endorse or promote products derived from this software
     20  1.6       agc  *    without specific prior written permission.
     21  1.6       agc  *
     22  1.6       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.6       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.6       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.6       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.6       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.6       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.6       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.6       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.6       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.6       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.6       agc  * SUCH DAMAGE.
     33  1.6       agc  */
     34  1.6       agc 
     35  1.6       agc /*-
     36  1.6       agc  * Copyright (c) 1991 Keith Muller.
     37  1.6       agc  *
     38  1.6       agc  * This code is derived from software contributed to Berkeley by
     39  1.6       agc  * Keith Muller of the University of California, San Diego.
     40  1.6       agc  *
     41  1.6       agc  * Redistribution and use in source and binary forms, with or without
     42  1.6       agc  * modification, are permitted provided that the following conditions
     43  1.6       agc  * are met:
     44  1.6       agc  * 1. Redistributions of source code must retain the above copyright
     45  1.6       agc  *    notice, this list of conditions and the following disclaimer.
     46  1.6       agc  * 2. Redistributions in binary form must reproduce the above copyright
     47  1.6       agc  *    notice, this list of conditions and the following disclaimer in the
     48  1.6       agc  *    documentation and/or other materials provided with the distribution.
     49  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     50  1.1       cgd  *    must display the following acknowledgement:
     51  1.1       cgd  *	This product includes software developed by the University of
     52  1.1       cgd  *	California, Berkeley and its contributors.
     53  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     54  1.1       cgd  *    may be used to endorse or promote products derived from this software
     55  1.1       cgd  *    without specific prior written permission.
     56  1.1       cgd  *
     57  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  1.1       cgd  * SUCH DAMAGE.
     68  1.1       cgd  */
     69  1.1       cgd 
     70  1.3     lukem #include <sys/cdefs.h>
     71  1.1       cgd #ifndef lint
     72  1.3     lukem #if 0
     73  1.3     lukem from: static char sccsid[] = "@(#)egetopt.c	8.1 (Berkeley) 6/6/93";
     74  1.3     lukem #else
     75  1.6       agc __RCSID("$NetBSD: egetopt.c,v 1.6 2003/08/07 11:15:29 agc Exp $");
     76  1.3     lukem #endif
     77  1.1       cgd #endif /* not lint */
     78  1.1       cgd 
     79  1.1       cgd #include <ctype.h>
     80  1.1       cgd #include <stdio.h>
     81  1.1       cgd #include <stdlib.h>
     82  1.1       cgd #include <string.h>
     83  1.1       cgd 
     84  1.1       cgd #include "extern.h"
     85  1.1       cgd 
     86  1.1       cgd /*
     87  1.1       cgd  * egetopt:	get option letter from argument vector (an extended
     88  1.1       cgd  *		version of getopt).
     89  1.1       cgd  *
     90  1.1       cgd  * Non standard additions to the ostr specs are:
     91  1.1       cgd  * 1) '?': immediate value following arg is optional (no white space
     92  1.1       cgd  *    between the arg and the value)
     93  1.1       cgd  * 2) '#': +/- followed by a number (with an optional sign but
     94  1.1       cgd  *    no white space between the arg and the number). The - may be
     95  1.1       cgd  *    combined with other options, but the + cannot.
     96  1.1       cgd  */
     97  1.1       cgd 
     98  1.1       cgd int	eopterr = 1;		/* if error message should be printed */
     99  1.1       cgd int	eoptind = 1;		/* index into parent argv vector */
    100  1.1       cgd int	eoptopt;		/* character checked for validity */
    101  1.1       cgd char	*eoptarg;		/* argument associated with option */
    102  1.1       cgd 
    103  1.1       cgd #define	BADCH	(int)'?'
    104  1.1       cgd #define	EMSG	""
    105  1.1       cgd 
    106  1.1       cgd int
    107  1.1       cgd egetopt(nargc, nargv, ostr)
    108  1.1       cgd 	int nargc;
    109  1.1       cgd 	char * const *nargv;
    110  1.1       cgd 	const char *ostr;
    111  1.1       cgd {
    112  1.1       cgd 	static char *place = EMSG;	/* option letter processing */
    113  1.3     lukem 	char *oli;			/* option letter list index */
    114  1.5       wiz 	static int delim;		/* which option delimiter */
    115  1.3     lukem 	char *p;
    116  1.1       cgd 	static char savec = '\0';
    117  1.1       cgd 
    118  1.1       cgd 	if (savec != '\0') {
    119  1.1       cgd 		*place = savec;
    120  1.1       cgd 		savec = '\0';
    121  1.1       cgd 	}
    122  1.1       cgd 
    123  1.1       cgd 	if (!*place) {
    124  1.1       cgd 		/*
    125  1.1       cgd 		 * update scanning pointer
    126  1.1       cgd 		 */
    127  1.1       cgd 		if ((eoptind >= nargc) ||
    128  1.1       cgd 		    ((*(place = nargv[eoptind]) != '-') && (*place != '+'))) {
    129  1.1       cgd 			place = EMSG;
    130  1.3     lukem 			return (-1);
    131  1.1       cgd 		}
    132  1.1       cgd 
    133  1.1       cgd 		delim = (int)*place;
    134  1.1       cgd 		if (place[1] && *++place == '-' && !place[1]) {
    135  1.1       cgd 			/*
    136  1.1       cgd 			 * found "--"
    137  1.1       cgd 			 */
    138  1.1       cgd 			++eoptind;
    139  1.1       cgd 			place = EMSG;
    140  1.3     lukem 			return (-1);
    141  1.1       cgd 		}
    142  1.1       cgd 	}
    143  1.1       cgd 
    144  1.1       cgd 	/*
    145  1.1       cgd 	 * check option letter
    146  1.1       cgd 	 */
    147  1.1       cgd 	if ((eoptopt = (int)*place++) == (int)':' || (eoptopt == (int)'?') ||
    148  1.1       cgd 	    !(oli = strchr(ostr, eoptopt))) {
    149  1.1       cgd 		/*
    150  1.1       cgd 		 * if the user didn't specify '-' as an option,
    151  1.3     lukem 		 * assume it means -1 when by itself.
    152  1.1       cgd 		 */
    153  1.1       cgd 		if ((eoptopt == (int)'-') && !*place)
    154  1.3     lukem 			return (-1);
    155  1.1       cgd 		if (strchr(ostr, '#') && (isdigit(eoptopt) ||
    156  1.1       cgd 		    (((eoptopt == (int)'-') || (eoptopt == (int)'+')) &&
    157  1.4  christos 		      isdigit((unsigned char)*place)))) {
    158  1.1       cgd 			/*
    159  1.1       cgd 			 * # option: +/- with a number is ok
    160  1.1       cgd 			 */
    161  1.1       cgd 			for (p = place; *p != '\0'; ++p) {
    162  1.4  christos 				if (!isdigit((unsigned char)*p))
    163  1.1       cgd 					break;
    164  1.1       cgd 			}
    165  1.1       cgd 			eoptarg = place-1;
    166  1.1       cgd 
    167  1.1       cgd 			if (*p == '\0') {
    168  1.1       cgd 				place = EMSG;
    169  1.1       cgd 				++eoptind;
    170  1.1       cgd 			} else {
    171  1.1       cgd 				place = p;
    172  1.1       cgd 				savec = *p;
    173  1.1       cgd 				*place = '\0';
    174  1.1       cgd 			}
    175  1.1       cgd 			return (delim);
    176  1.1       cgd 		}
    177  1.1       cgd 
    178  1.1       cgd 		if (!*place)
    179  1.1       cgd 			++eoptind;
    180  1.1       cgd 		if (eopterr) {
    181  1.1       cgd 			if (!(p = strrchr(*nargv, '/')))
    182  1.1       cgd 				p = *nargv;
    183  1.1       cgd 			else
    184  1.1       cgd 				++p;
    185  1.1       cgd 			(void)fprintf(stderr, "%s: illegal option -- %c\n",
    186  1.1       cgd 			    p, eoptopt);
    187  1.1       cgd 		}
    188  1.1       cgd 		return (BADCH);
    189  1.1       cgd 	}
    190  1.1       cgd 	if (delim == (int)'+') {
    191  1.1       cgd 		/*
    192  1.1       cgd 		 * '+' is only allowed with numbers
    193  1.1       cgd 		 */
    194  1.1       cgd 		if (!*place)
    195  1.1       cgd 			++eoptind;
    196  1.1       cgd 		if (eopterr) {
    197  1.1       cgd 			if (!(p = strrchr(*nargv, '/')))
    198  1.1       cgd 				p = *nargv;
    199  1.1       cgd 			else
    200  1.1       cgd 				++p;
    201  1.1       cgd 			(void)fprintf(stderr,
    202  1.1       cgd 				"%s: illegal '+' delimiter with option -- %c\n",
    203  1.1       cgd 				p, eoptopt);
    204  1.1       cgd 		}
    205  1.1       cgd 		return (BADCH);
    206  1.1       cgd 	}
    207  1.1       cgd 	++oli;
    208  1.1       cgd 	if ((*oli != ':') && (*oli != '?')) {
    209  1.1       cgd 		/*
    210  1.1       cgd 		 * don't need argument
    211  1.1       cgd 		 */
    212  1.1       cgd 		eoptarg = NULL;
    213  1.1       cgd 		if (!*place)
    214  1.1       cgd 			++eoptind;
    215  1.1       cgd 		return (eoptopt);
    216  1.1       cgd 	}
    217  1.1       cgd 
    218  1.1       cgd 	if (*place) {
    219  1.1       cgd 		/*
    220  1.1       cgd 		 * no white space
    221  1.1       cgd 		 */
    222  1.1       cgd 		eoptarg = place;
    223  1.1       cgd 	} else if (*oli == '?') {
    224  1.1       cgd 		/*
    225  1.1       cgd 		 * no arg, but NOT required
    226  1.1       cgd 		 */
    227  1.1       cgd 		eoptarg = NULL;
    228  1.1       cgd 	} else if (nargc <= ++eoptind) {
    229  1.1       cgd 		/*
    230  1.1       cgd 		 * no arg, but IS required
    231  1.1       cgd 		 */
    232  1.1       cgd 		place = EMSG;
    233  1.1       cgd 		if (eopterr) {
    234  1.1       cgd 			if (!(p = strrchr(*nargv, '/')))
    235  1.1       cgd 				p = *nargv;
    236  1.1       cgd 			else
    237  1.1       cgd 				++p;
    238  1.1       cgd 			(void)fprintf(stderr,
    239  1.1       cgd 			    "%s: option requires an argument -- %c\n", p,
    240  1.1       cgd 			    eoptopt);
    241  1.1       cgd 		}
    242  1.1       cgd 		return (BADCH);
    243  1.1       cgd 	} else {
    244  1.1       cgd 		/*
    245  1.1       cgd 		 * arg has white space
    246  1.1       cgd 		 */
    247  1.1       cgd 		eoptarg = nargv[eoptind];
    248  1.1       cgd 	}
    249  1.1       cgd 	place = EMSG;
    250  1.1       cgd 	++eoptind;
    251  1.1       cgd 	return (eoptopt);
    252  1.1       cgd }
    253