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