Example #1
0
int
getopt_long_only(int argc, char **argv,
                     char *options,
		     struct option *long_options, int *opt_index)
{
  return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
}
Example #2
0
SDMLIB_API int getopt (int argc, char *const *argv, const char* optstring)
{
  return _getopt_internal (argc, argv, optstring,
			   (const struct option *) 0,
			   (int *) 0,
			   0);
}
Example #3
0
int
GetOptWrapper::getopt (int argc, char *const *argv, const char *optstring)
{
   return _getopt_internal (argc, argv, optstring,
      (const struct option *) 0,
      (int *) 0,
      0);
}
Example #4
0
int
getopt_long (int argc,
	     char *const *argv,
	     const char *options,
	     const struct option *long_options,
	     int *opt_index)
{
  return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
}
Example #5
0
/* Calls internal GetOptWrapper::getopt function to enable long option names.  */
int
GetOptWrapper::getopt_long (int argc, char *const *argv, const char *shortopts,
             const struct option *longopts, int *longind)
{
   return _getopt_internal (argc, argv, shortopts, longopts, longind, 0);
}
int getopt_long_only(int argc, char * argv[], const char *shortopts, const struct option *longopts, int *longind)
{
  return _getopt_internal(argc, argv, shortopts, longopts, longind, 1);
}
int _getopt_internal(int argc, char * argv[], const char *shortopts,
                     const struct option *longopts, int *longind,
                     int long_only)
{
  char mode, colon_mode = *shortopts;
  int shortoff = 0, opt = -1;

  if(getenv("POSIXLY_CORRECT")) colon_mode = mode = '+';
  else {
    if((colon_mode = *shortopts) == ':') shortoff ++;
    if(((mode = shortopts[shortoff]) == '+') || (mode == '-')) {
      shortoff++;
      if((colon_mode != ':') && ((colon_mode = shortopts[shortoff]) == ':'))
        shortoff ++;
    }
  }
  optarg = 0;
  if((optind >= argc) ||
      ((argv[optind][0] == '-') &&
       (argv[optind][1] == '-') &&
       (argv[optind][2] == '\0'))) {
    optind++;
    opt = -1;
  } else if((argv[optind][0] != '-') ||
            (argv[optind][1] == '\0')) {
    char *tmp;
    int i, j, k;

    opt = -1;
    if(mode == '+') return -1;
    else if(mode == '-') {
      optarg = argv[optind++];
      return 1;
    }
    for(i=j=optind; i<argc; i++) if((argv[i][0] == '-') &&
                                    (argv[i][1] != '\0')) {
      optind=i;
      opt=_getopt_internal(argc, argv, shortopts,
                              longopts, longind,
                              long_only);
      while(i > j) {
        tmp=argv[--i];
        for(k=i; k+1<optind; k++)
          argv[k]=argv[k+1];
        argv[--optind]=tmp;
      }
      break;
    }
  } else if((!long_only) && (argv[optind][1] != '-'))
    opt = getopt(argc, argv, shortopts);
  else {
    int charind, offset;
    int found = 0, ind, hits = 0;

    if(((optopt = argv[optind][1]) != '-') && ! argv[optind][2]) {
      int c;
      
      ind = shortoff;
      while((c = shortopts[ind++])) {
        if(((shortopts[ind] == ':') ||
            ((c == 'W') && (shortopts[ind] == ';'))) &&
           (shortopts[++ind] == ':'))
          ind ++;
        if(optopt == c) return getopt(argc, argv, shortopts);
      }
    }
    offset = 2 - (argv[optind][1] != '-');
    for(charind = offset;
        (argv[optind][charind] != '\0') &&
          (argv[optind][charind] != '=');
        charind++);
    for(ind = 0; longopts[ind].name && !hits; ind++)
      if(((int)strlen(longopts[ind].name) == (charind - offset)) &&
         (strncmp(longopts[ind].name,
                  argv[optind] + offset, charind - offset) == 0))
        found = ind, hits++;
    if(!hits) for(ind = 0; longopts[ind].name; ind++)
      if(strncmp(longopts[ind].name,
                 argv[optind] + offset, charind - offset) == 0)
        found = ind, hits++;
    if(hits == 1) {
      opt = 0;

      if(argv[optind][charind] == '=') {
        if(longopts[found].has_arg == 0) {
          opt = '?';
          if(opterr) fprintf(stderr,
                             "%s: option `--%s' doesn't allow an argument\n",
                             argv[0], longopts[found].name);
        } else {
          optarg = argv[optind] + ++charind;
          charind = 0;
        }
      } else if(longopts[found].has_arg == 1) {
        if(++optind >= argc) {
          opt = (colon_mode == ':') ? ':' : '?';
          if(opterr) fprintf(stderr,
                             "%s: option `--%s' requires an argument\n",
                             argv[0], longopts[found].name);
        } else optarg = argv[optind];
      }
      if(!opt) {
        if (longind) *longind = found;
        if(!longopts[found].flag) opt = longopts[found].val;
        else *(longopts[found].flag) = longopts[found].val;
      }
      optind++;
    } else if(!hits) {
      if(offset == 1) opt = getopt(argc, argv, shortopts);
      else {
        opt = '?';
        if(opterr) fprintf(stderr,
                           "%s: unrecognized option `%s'\n",
                           argv[0], argv[optind++]);
      }
    } else {
      opt = '?';
      if(opterr) fprintf(stderr,
                         "%s: option `%s' is ambiguous\n",
                         argv[0], argv[optind++]);
    }
  }
  if (optind > argc) optind = argc;
  return opt;
}
Example #8
0
int getopt_long_only(int argc, TCHAR *const *argv, const TCHAR *options, const struct option *long_options, int *opt_index)
{
	return _getopt_internal(argc, argv, options, long_options, opt_index, true);
}
Example #9
0
int getopt_short(int argc, TCHAR *const *argv, const TCHAR *optstring)
{
	return _getopt_internal(argc, argv, optstring, NULL, NULL, false);
}
Example #10
0
SDMLIB_API int getopt_long_only (int argc, char *const *argv, const char* optstring,
		        const struct option *longopts, int *longind)
{
  return _getopt_internal (argc, argv, optstring,
			   longopts, longind, 1);
}