Home | History | Annotate | Line # | Download | only in stdlib
getopt_long.c revision 1.1
      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 <stdio.h>
     35 #include <stdlib.h>
     36 #include <string.h>
     37 #include <sys/cdefs.h>
     38 #include "getopt.h"
     39 
     40 extern int	  opterr;	/* if error message should be printed */
     41 extern int	  optind;	/* index into parent argv vector */
     42 extern int	  optopt;	/* character checked for validity */
     43 extern int	  optreset;	/* reset getopt */
     44 extern char *optarg;	/* argument associated with option */
     45 
     46 static char * __progname __P((char *));
     47 int getopt_internal __P((int, char * const *, const char *));
     48 
     49 static char * __progname(nargv0)
     50     char * nargv0;
     51 {
     52     char * tmp = strrchr(nargv0, '/');
     53     if (tmp) tmp++; else tmp = nargv0;
     54     return(tmp);
     55 }
     56 
     57 #define	BADCH	(int)'?'
     58 #define	BADARG	(int)':'
     59 #define	EMSG	""
     60 
     61 /*
     62  * getopt --
     63  *	Parse argc/argv argument vector.
     64  */
     65 int
     66 getopt_internal(nargc, nargv, ostr)
     67 	int nargc;
     68 	char * const *nargv;
     69 	const char *ostr;
     70 {
     71 	static char *place = EMSG;		/* option letter processing */
     72 	char *oli;				/* option letter list index */
     73 
     74 	if (optreset || !*place) {		/* update scanning pointer */
     75 		optreset = 0;
     76 		if (optind >= nargc || *(place = nargv[optind]) != '-') {
     77 			place = EMSG;
     78 			return (-1);
     79 		}
     80 		if (place[1] && *++place == '-') {	/* found "--" */
     81 			/* ++optind; */
     82 			place = EMSG;
     83 			return (-2);
     84 		}
     85 	}					/* option letter okay? */
     86 	if ((optopt = (int)*place++) == (int)':' ||
     87 	    !(oli = strchr(ostr, optopt))) {
     88 		/*
     89 		 * if the user didn't specify '-' as an option,
     90 		 * assume it means -1.
     91 		 */
     92 		if (optopt == (int)'-')
     93 			return (-1);
     94 		if (!*place)
     95 			++optind;
     96 		if (opterr && *ostr != ':')
     97 			(void)fprintf(stderr,
     98 			    "%s: illegal option -- %c\n", __progname(nargv[0]), optopt);
     99 		return (BADCH);
    100 	}
    101 	if (*++oli != ':') {			/* don't need argument */
    102 		optarg = NULL;
    103 		if (!*place)
    104 			++optind;
    105 	} else {				/* need an argument */
    106 		if (*place)			/* no white space */
    107 			optarg = place;
    108 		else if (nargc <= ++optind) {	/* no arg */
    109 			place = EMSG;
    110 			if ((opterr) && (*ostr != ':'))
    111 				(void)fprintf(stderr,
    112 				    "%s: option requires an argument -- %c\n",
    113 				    __progname(nargv[0]), optopt);
    114 			return (BADARG);
    115 		} else				/* white space */
    116 			optarg = nargv[optind];
    117 		place = EMSG;
    118 		++optind;
    119 	}
    120 	return (optopt);			/* dump back option letter */
    121 }
    122 
    123 #if 0
    124 /*
    125  * getopt --
    126  *	Parse argc/argv argument vector.
    127  */
    128 int
    129 getopt2(nargc, nargv, ostr)
    130 	int nargc;
    131 	char * const *nargv;
    132 	const char *ostr;
    133 {
    134 	int retval;
    135 
    136 	if ((retval = getopt_internal(nargc, nargv, ostr)) == -2) {
    137 		retval = -1;
    138 		++optind;
    139 	}
    140 	return(retval);
    141 }
    142 #endif
    143 
    144 /*
    145  * getopt_long --
    146  *	Parse argc/argv argument vector.
    147  */
    148 int
    149 getopt_long(nargc, nargv, options, long_options, index)
    150      int nargc;
    151      char ** nargv;
    152      char * options;
    153      struct option * long_options;
    154      int * index;
    155 {
    156 	int retval;
    157 
    158 	if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
    159 		char *current_argv = nargv[optind++] + 2, *has_equal;
    160 		int i, current_argv_len, match = -1;
    161 
    162 		if (*current_argv == '\0') {
    163 			return(-1);
    164 		}
    165 		if ((has_equal = strchr(current_argv, '=')) != NULL) {
    166 			current_argv_len = has_equal - current_argv;
    167 			has_equal++;
    168 		} else
    169 			current_argv_len = strlen(current_argv);
    170 
    171 		for (i = 0; long_options[i].name; i++) {
    172 			if (strncmp(current_argv, long_options[i].name, current_argv_len))
    173 				continue;
    174 
    175 			if (strlen(long_options[i].name) == (unsigned)current_argv_len) {
    176 				match = i;
    177 				break;
    178 			}
    179 			if (match == -1)
    180 				match = i;
    181 		}
    182 		if (match != -1) {
    183 			if (long_options[match].has_arg == required_argument ||
    184 			    long_options[match].has_arg == optional_argument) {
    185 				if (has_equal)
    186 					optarg = has_equal;
    187 				else
    188 					optarg = nargv[optind++];
    189 			}
    190 			if ((long_options[match].has_arg == required_argument)
    191 			    && (optarg == NULL)) {
    192 				/*
    193 				 * Missing argument, leading :
    194 				 * indicates no error should be generated
    195 				 */
    196 				if ((opterr) && (*options != ':'))
    197 					(void)fprintf(stderr,
    198 				      "%s: option requires an argument -- %s\n",
    199 				      __progname(nargv[0]), current_argv);
    200 				return (BADARG);
    201 			}
    202 		} else { /* No matching argument */
    203 			if ((opterr) && (*options != ':'))
    204 				(void)fprintf(stderr,
    205 				    "%s: illegal option -- %s\n", __progname(nargv[0]), current_argv);
    206 			return (BADCH);
    207 		}
    208 		if (long_options[match].flag) {
    209 			*long_options[match].flag = long_options[match].val;
    210 			retval = 0;
    211 		} else
    212 			retval = long_options[match].val;
    213 		if (index)
    214 			*index = match;
    215 	}
    216 	return(retval);
    217 }
    218 
    219