#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)opj_getopt.c 8.3 (Berkeley) 4/27/95";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "opj_getopt.h"
int opj_opterr = 1,
opj_optind = 1,
opj_optopt,
opj_optreset;
char *opj_optarg;
#define BADCH (int)'?'
#define BADARG (int)':'
static char EMSG[] = {""};
void opj_reset_options_reading(void)
{
opj_opterr = 1;
opj_optind = 1;
}
int opj_getopt(int nargc, char *const *nargv, const char *ostr)
{
# define __progname nargv[0]
static char *place = EMSG;
const char *oli = NULL;
if (opj_optreset || !*place) {
opj_optreset = 0;
if (opj_optind >= nargc || *(place = nargv[opj_optind]) != '-') {
place = EMSG;
return (-1);
}
if (place[1] && *++place == '-') {
++opj_optind;
place = EMSG;
return (-1);
}
}
if ((opj_optopt = (int) * place++) == (int) ':' ||
!(oli = strchr(ostr, opj_optopt))) {
if (opj_optopt == (int) '-') {
return (-1);
}
if (!*place) {
++opj_optind;
}
if (opj_opterr && *ostr != ':') {
fprintf(stderr,
"%s: illegal option -- %c\n", __progname, opj_optopt);
return (BADCH);
}
}
if (*++oli != ':') {
opj_optarg = NULL;
if (!*place) {
++opj_optind;
}
} else {
if (*place) {
opj_optarg = place;
} else if (nargc <= ++opj_optind) {
place = EMSG;
if (*ostr == ':') {
return (BADARG);
}
if (opj_opterr) {
fprintf(stderr,
"%s: option requires an argument -- %c\n",
__progname, opj_optopt);
return (BADCH);
}
} else {
opj_optarg = nargv[opj_optind];
}
place = EMSG;
++opj_optind;
}
return (opj_optopt);
}
int opj_getopt_long(int argc, char * const argv[], const char *optstring,
const opj_option_t *longopts, int totlen)
{
static int lastidx, lastofs;
const char *tmp;
int i, len;
char param = 1;
again:
if (opj_optind >= argc || !argv[opj_optind] || *argv[opj_optind] != '-') {
return -1;
}
if (argv[opj_optind][0] == '-' && argv[opj_optind][1] == 0) {
if (opj_optind >= (argc - 1)) {
param = 0;
} else {
if (argv[opj_optind + 1][0] == '-') {
param = 0;
} else {
param = 2;
}
}
}
if (param == 0) {
++opj_optind;
return (BADCH);
}
if (argv[opj_optind][0] == '-') {
char* arg;
const opj_option_t* o;
o = longopts;
len = sizeof(longopts[0]);
if (param > 1) {
if (opj_optind + 1 >= argc) {
return -1;
}
arg = argv[opj_optind + 1];
opj_optind++;
} else {
arg = argv[opj_optind] + 1;
}
if (strlen(arg) > 1) {
for (i = 0; i < totlen; i = i + len, o++) {
if (!strcmp(o->name, arg)) {
if (o->has_arg == 0) {
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;
}
} else {
opj_optarg = argv[opj_optind + 1];
if (opj_optarg) {
if (opj_optarg[0] ==
'-') {
if (opj_opterr) {
fprintf(stderr, "%s: option requires an argument\n", arg);
return (BADCH);
}
}
}
if (!opj_optarg && o->has_arg == 1) {
if (opj_opterr) {
fprintf(stderr, "%s: option requires an argument \n", arg);
return (BADCH);
}
}
++opj_optind;
}
++opj_optind;
if (o->flag) {
*(o->flag) = o->val;
} else {
return o->val;
}
return 0;
}
}
fprintf(stderr, "Invalid option %s\n", arg);
++opj_optind;
return (BADCH);
} else {
if (*optstring == ':') {
return ':';
}
if (lastidx != opj_optind) {
lastidx = opj_optind;
lastofs = 0;
}
opj_optopt = argv[opj_optind][lastofs + 1];
if ((tmp = strchr(optstring, opj_optopt))) {
if (*tmp == 0) {
++opj_optind;
goto again;
}
if (tmp[1] == ':') {
if (tmp[2] == ':' ||
argv[opj_optind][lastofs + 2]) {
if (!*(opj_optarg = argv[opj_optind] + lastofs + 2)) {
opj_optarg = 0;
}
goto found;
}
opj_optarg = argv[opj_optind + 1];
if (opj_optarg) {
if (opj_optarg[0] ==
'-') {
if (opj_opterr) {
fprintf(stderr, "%s: option requires an argument\n", arg);
++opj_optind;
return (BADCH);
}
}
}
if (!opj_optarg) {
if (opj_opterr) {
fprintf(stderr, "%s: option requires an argument\n", arg);
++opj_optind;
return (BADCH);
}
}
++opj_optind;
} else {
++lastofs;
return opj_optopt;
}
found:
++opj_optind;
return opj_optopt;
} else {
fprintf(stderr, "Invalid option %s\n", arg);
++opj_optind;
return (BADCH);
}
}
}
fprintf(stderr, "Invalid option\n");
++opj_optind;
return (BADCH);;
}