2010-12-08 12:06:41 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1987, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* last review : october 29th, 2002 */
|
|
|
|
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
2011-09-01 14:08:10 +02:00
|
|
|
static char sccsid[] = "@(#)opj_getopt.c 8.3 (Berkeley) 4/27/95";
|
2010-12-08 12:06:41 +01:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-09-01 14:08:10 +02:00
|
|
|
#include "opj_getopt.h"
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2011-09-01 14:08:10 +02:00
|
|
|
int opj_opterr = 1, /* if error message should be printed */
|
|
|
|
opj_optind = 1, /* index into parent argv vector */
|
|
|
|
opj_optopt, /* character checked for validity */
|
|
|
|
opj_optreset; /* reset getopt */
|
|
|
|
char *opj_optarg; /* argument associated with option */
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
#define BADCH (int)'?'
|
|
|
|
#define BADARG (int)':'
|
2011-09-01 14:08:10 +02:00
|
|
|
static char EMSG[]={""};
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
/* As this class remembers its values from one Java call to the other, reset the values before each use */
|
2011-07-29 10:50:55 +02:00
|
|
|
void reset_options_reading(void) {
|
2011-09-01 14:08:10 +02:00
|
|
|
opj_opterr = 1;
|
|
|
|
opj_optind = 1;
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getopt --
|
|
|
|
* Parse argc/argv argument vector.
|
|
|
|
*/
|
2011-09-01 14:08:10 +02:00
|
|
|
int opj_getopt(int nargc, char *const *nargv, const char *ostr) {
|
2010-12-08 12:06:41 +01:00
|
|
|
# define __progname nargv[0]
|
2011-09-01 14:08:10 +02:00
|
|
|
static char *place = EMSG; /* option letter processing */
|
2011-12-08 10:25:25 +01:00
|
|
|
char *oli = NULL; /* option letter list index */
|
2010-12-08 12:06:41 +01:00
|
|
|
|
2011-09-01 14:08:10 +02:00
|
|
|
if (opj_optreset || !*place) { /* update scanning pointer */
|
|
|
|
opj_optreset = 0;
|
|
|
|
if (opj_optind >= nargc || *(place = nargv[opj_optind]) != '-') {
|
2010-12-08 12:06:41 +01:00
|
|
|
place = EMSG;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (place[1] && *++place == '-') { /* found "--" */
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
place = EMSG;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} /* option letter okay? */
|
2011-09-01 14:08:10 +02:00
|
|
|
if ((opj_optopt = (int) *place++) == (int) ':' ||
|
|
|
|
!(oli = strchr(ostr, opj_optopt))) {
|
2010-12-08 12:06:41 +01:00
|
|
|
/*
|
|
|
|
* if the user didn't specify '-' as an option,
|
|
|
|
* assume it means -1.
|
|
|
|
*/
|
2011-09-01 14:08:10 +02:00
|
|
|
if (opj_optopt == (int) '-')
|
2010-12-08 12:06:41 +01:00
|
|
|
return (-1);
|
|
|
|
if (!*place)
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
|
|
|
if (opj_opterr && *ostr != ':') {
|
2010-12-08 12:06:41 +01:00
|
|
|
fprintf(stderr,
|
2011-09-01 14:08:10 +02:00
|
|
|
"%s: illegal option -- %c\n", __progname, opj_optopt);
|
2010-12-08 12:06:41 +01:00
|
|
|
return (BADCH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*++oli != ':') { /* don't need argument */
|
2011-09-01 14:08:10 +02:00
|
|
|
opj_optarg = NULL;
|
2010-12-08 12:06:41 +01:00
|
|
|
if (!*place)
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
} else { /* need an argument */
|
|
|
|
if (*place) /* no white space */
|
2011-09-01 14:08:10 +02:00
|
|
|
opj_optarg = place;
|
|
|
|
else if (nargc <= ++opj_optind) { /* no arg */
|
2010-12-08 12:06:41 +01:00
|
|
|
place = EMSG;
|
|
|
|
if (*ostr == ':')
|
|
|
|
return (BADARG);
|
2011-09-01 14:08:10 +02:00
|
|
|
if (opj_opterr) {
|
2010-12-08 12:06:41 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: option requires an argument -- %c\n",
|
2011-09-01 14:08:10 +02:00
|
|
|
__progname, opj_optopt);
|
2010-12-08 12:06:41 +01:00
|
|
|
return (BADCH);
|
|
|
|
}
|
|
|
|
} else /* white space */
|
2011-09-01 14:08:10 +02:00
|
|
|
opj_optarg = nargv[opj_optind];
|
2010-12-08 12:06:41 +01:00
|
|
|
place = EMSG;
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
2011-09-01 14:08:10 +02:00
|
|
|
return (opj_optopt); /* dump back option letter */
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-01 14:08:10 +02:00
|
|
|
int opj_getopt_long(int argc, char * const argv[], const char *optstring,
|
|
|
|
const opj_option_t *longopts, int totlen) {
|
2010-12-08 12:06:41 +01:00
|
|
|
static int lastidx,lastofs;
|
|
|
|
char *tmp;
|
|
|
|
int i,len;
|
|
|
|
char param = 1;
|
|
|
|
|
|
|
|
again:
|
2011-09-01 14:08:10 +02:00
|
|
|
if (opj_optind >= argc || !argv[opj_optind] || *argv[opj_optind]!='-')
|
2010-12-08 12:06:41 +01:00
|
|
|
return -1;
|
|
|
|
|
2011-09-01 14:08:10 +02:00
|
|
|
if (argv[opj_optind][0]=='-' && argv[opj_optind][1]==0) {
|
|
|
|
if(opj_optind >= (argc - 1)){ /* no more input parameters */
|
2010-12-08 12:06:41 +01:00
|
|
|
param = 0;
|
|
|
|
}
|
|
|
|
else{ /* more input parameters */
|
2011-09-01 14:08:10 +02:00
|
|
|
if(argv[opj_optind + 1][0] == '-'){
|
2010-12-08 12:06:41 +01:00
|
|
|
param = 0; /* Missing parameter after '-' */
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
param = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param == 0) {
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
return (BADCH);
|
|
|
|
}
|
|
|
|
|
2011-09-01 14:08:10 +02:00
|
|
|
if (argv[opj_optind][0]=='-') { /* long option */
|
|
|
|
char* arg=argv[opj_optind]+1;
|
|
|
|
const opj_option_t* o;
|
2010-12-08 12:06:41 +01:00
|
|
|
o=longopts;
|
|
|
|
len=sizeof(longopts[0]);
|
|
|
|
|
|
|
|
if (param > 1){
|
2011-09-01 14:08:10 +02:00
|
|
|
arg = argv[opj_optind+1];
|
|
|
|
opj_optind++;
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
else
|
2011-09-01 14:08:10 +02:00
|
|
|
arg = argv[opj_optind]+1;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
|
|
if(strlen(arg)>1){
|
|
|
|
for (i=0;i<totlen;i=i+len,o++) {
|
|
|
|
if (!strcmp(o->name,arg)) { /* match */
|
|
|
|
if (o->has_arg == 0) {
|
2011-09-01 14:08:10 +02:00
|
|
|
if ((argv[opj_optind+1])&&(!(argv[opj_optind+1][0]=='-'))){
|
|
|
|
fprintf(stderr,"%s: option does not require an argument. Ignoring %s\n",arg,argv[opj_optind+1]);
|
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
}else{
|
2011-09-01 14:08:10 +02:00
|
|
|
opj_optarg=argv[opj_optind+1];
|
|
|
|
if(opj_optarg){
|
|
|
|
if (opj_optarg[0] == '-'){ /* Has read next input parameter: No arg for current parameter */
|
|
|
|
if (opj_opterr) {
|
2010-12-08 12:06:41 +01:00
|
|
|
fprintf(stderr,"%s: option requires an argument\n",arg);
|
|
|
|
return (BADCH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-01 14:08:10 +02:00
|
|
|
if (!opj_optarg && o->has_arg==1) { /* no argument there */
|
|
|
|
if (opj_opterr) {
|
2010-12-08 12:06:41 +01:00
|
|
|
fprintf(stderr,"%s: option requires an argument \n",arg);
|
|
|
|
return (BADCH);
|
|
|
|
}
|
|
|
|
}
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
if (o->flag)
|
|
|
|
*(o->flag)=o->val;
|
|
|
|
else
|
|
|
|
return o->val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}//(end for)String not found in the list
|
|
|
|
fprintf(stderr,"Invalid option %s\n",arg);
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
return (BADCH);
|
|
|
|
}else{ /*Single character input parameter*/
|
|
|
|
if (*optstring==':') return ':';
|
2011-09-01 14:08:10 +02:00
|
|
|
if (lastidx!=opj_optind) {
|
|
|
|
lastidx=opj_optind; lastofs=0;
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
2011-09-01 14:08:10 +02:00
|
|
|
opj_optopt=argv[opj_optind][lastofs+1];
|
|
|
|
if ((tmp=strchr(optstring,opj_optopt))) {/*Found input parameter in list*/
|
2010-12-08 12:06:41 +01:00
|
|
|
if (*tmp==0) { /* apparently, we looked for \0, i.e. end of argument */
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
if (tmp[1]==':') { /* argument expected */
|
2011-09-01 14:08:10 +02:00
|
|
|
if (tmp[2]==':' || argv[opj_optind][lastofs+2]) { /* "-foo", return "oo" as opj_optarg */
|
|
|
|
if (!*(opj_optarg=argv[opj_optind]+lastofs+2)) opj_optarg=0;
|
2010-12-08 12:06:41 +01:00
|
|
|
goto found;
|
|
|
|
}
|
2011-09-01 14:08:10 +02:00
|
|
|
opj_optarg=argv[opj_optind+1];
|
|
|
|
if(opj_optarg){
|
|
|
|
if (opj_optarg[0] == '-'){ /* Has read next input parameter: No arg for current parameter */
|
|
|
|
if (opj_opterr) {
|
2010-12-08 12:06:41 +01:00
|
|
|
fprintf(stderr,"%s: option requires an argument\n",arg);
|
|
|
|
return (BADCH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-01 14:08:10 +02:00
|
|
|
if (!opj_optarg) { /* missing argument */
|
|
|
|
if (opj_opterr) {
|
2010-12-08 12:06:41 +01:00
|
|
|
fprintf(stderr,"%s: option requires an argument\n",arg);
|
|
|
|
return (BADCH);
|
|
|
|
}
|
|
|
|
}
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
}else {/*Argument not expected*/
|
|
|
|
++lastofs;
|
2011-09-01 14:08:10 +02:00
|
|
|
return opj_optopt;
|
2010-12-08 12:06:41 +01:00
|
|
|
}
|
|
|
|
found:
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
|
|
|
return opj_optopt;
|
2010-12-08 12:06:41 +01:00
|
|
|
} else { /* not found */
|
|
|
|
fprintf(stderr,"Invalid option %s\n",arg);
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
return (BADCH);
|
|
|
|
}//end of not found
|
|
|
|
|
|
|
|
}// end of single character
|
|
|
|
}//end '-'
|
|
|
|
fprintf(stderr,"Invalid option\n");
|
2011-09-01 14:08:10 +02:00
|
|
|
++opj_optind;
|
2010-12-08 12:06:41 +01:00
|
|
|
return (BADCH);;
|
|
|
|
}//end function
|