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