Exemplo n.º 1
0
int LuaArgs::getp(int idx, const char *key, const char **list, const char *Default)
{
	const char *opt = getS(idx, key, "");
	int ret = findopt(opt, list, Default);
	delete[] opt;
	return ret;
}
Exemplo n.º 2
0
/*
 * Handle a char of an option toggle command.
 */
static int
mca_opt_char(int c)
{
	PARG parg;

	/*
	 * This may be a short option (single char),
	 * or one char of a long option name,
	 * or one char of the option parameter.
	 */
	if (curropt == NULL && len_cmdbuf() == 0) {
		int ret = mca_opt_first_char(c);
		if (ret != NO_MCA)
			return (ret);
	}
	if (optgetname) {
		/* We're getting a long option name.  */
		if (c != '\n' && c != '\r')
			return (mca_opt_nonfirst_char(c));
		if (curropt == NULL) {
			parg.p_string = get_cmdbuf();
			error("There is no --%s option", &parg);
			return (MCA_DONE);
		}
		optgetname = FALSE;
		cmd_reset();
	} else {
		if (is_erase_char(c))
			return (NO_MCA);
		if (curropt != NULL)
			/* We're getting the option parameter. */
			return (NO_MCA);
		curropt = findopt(c);
		if (curropt == NULL) {
			parg.p_string = propt(c);
			error("There is no %s option", &parg);
			return (MCA_DONE);
		}
	}
	/*
	 * If the option which was entered does not take a
	 * parameter, toggle the option immediately,
	 * so user doesn't have to hit RETURN.
	 */
	if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE ||
	    !opt_has_param(curropt)) {
		toggle_option(curropt, islower(c), "", optflag);
		return (MCA_DONE);
	}
	/*
	 * Display a prompt appropriate for the option parameter.
	 */
	start_mca(A_OPT_TOGGLE, opt_prompt(curropt), NULL, 0);
	return (MCA_MORE);
}
Exemplo n.º 3
0
int
main(int argc, char **argv)
{
	FILS	fstr[NFILES];
	int	nfdone = 0;


	/* Get locale variables for environment */
	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
#endif
	(void) textdomain(TEXT_DOMAIN);

	mbcurmax = MB_CUR_MAX;
	Files = fstr;
	for (argc = findopt(argc, argv); argc > 0; --argc, ++argv) {
		if (Multi == 'm') {
			if (Nfiles >= NFILES - 1) die("too many files");
			if (mustopen(*argv, &Files[Nfiles++]) == NULL)
				++nfdone;	/* suppress printing */
		} else {
			if (print(*argv))
				(void) fclose(Files->f_f);
			++nfdone;
		}
	}
	if (!nfdone)	/* no files named, use stdin */
		(void) print(NOFILE);	/* on GCOS, use current file, if any */

	if (Report) {
		errprint();	/* print accumulated error reports */
		exit(Error);
	}

	return (Error);
}
Exemplo n.º 4
0
Arquivo: pr.c Projeto: 00001/plan9port
void
main(int argc, char *argv[])
{
	Fils fstr[NFILES];
	int nfdone = 0;

	Binit(&bout, 1, OWRITE);
	Files = fstr;
	for(argc = findopt(argc, argv); argc > 0; --argc, ++argv)
		if(Multi == 'm') {
			if(Nfiles >= NFILES - 1)
				die("too many files");
			if(mustopen(*argv, &Files[Nfiles++]) == 0)
				nfdone++; /* suppress printing */
		} else {
			if(pr(*argv))
				Bterm(Files->f_f);
			nfdone++;
		}
	if(!nfdone)			/* no files named, use stdin */
		pr(nulls);		/* on GCOS, use current file, if any */
	errprint();			/* print accumulated error reports */
	exits(error? "error": 0);
}
Exemplo n.º 5
0
MP_GLOBAL
int
__mp_getopt(unsigned long n, char **a, char *s, option *l)
{
    static char *t;
    option *m;
    char *b, *p;
    int r;

    __mp_optarg = NULL;
    /* If the index of the current option is zero or if there are no more
     * options in this argument then we proceed to the next argument.
     */
    if ((__mp_optindex == 0) || ((t != NULL) && (*t == '\0')))
    {
        __mp_optindex++;
        t = NULL;
    }
    /* Get the basename of the filename that the program was invoked with so
     * that we can use that for any diagnostics.
     */
    b = __mp_basename(a[0]);
    /* If there is not a current option then attempt to locate it, otherwise
     * return EOF if there are no more options.
     */
    if (t == NULL)
    {
        if (__mp_optindex >= n)
            return EOF;
        t = a[__mp_optindex];
        /* Stop parsing options if either the argument is not an option, if it
         * is a single dash (representing stdin) or if it is a double dash
         * representing the end of options.
         */
        if ((*t != '-') || (t[1] == '\0') || ((t[1] == '-') && (t[2] == '\0')))
        {
            if ((*t == '-') && (t[1] == '-') && (t[2] == '\0'))
                __mp_optindex++;
            t = NULL;
            return EOF;
        }
        t++;
        /* Parse a long option and possibly its argument.
         */
        if ((*t == '-') && (l != NULL))
        {
            t++;
            /* Check that the option appears in the long options table.
             */
            if ((m = findopt(t, l, &t)) == NULL)
            {
                fprintf(stderr, "%s: Illegal option `--%s'\n", b, t);
                __mp_optindex++;
                t = NULL;
                return '?';
            }
            /* Check to see if the option takes an argument.
             */
            if (m->arg)
                if (*t == '\0')
                {
                    /* The rest of this argument is empty, so we proceed to the
                     * next argument.
                     */
                    if ((++__mp_optindex >= n) ||
                        (strcmp(a[__mp_optindex], "--") == 0))
                    {
                        fprintf(stderr, "%s: Option `--%s' requires an "
                                "argument\n", b, m->name);
                        t = NULL;
                        return '?';
                    }
                    __mp_optarg = a[__mp_optindex];
                }
                else
                    __mp_optarg = t;
            else if (*t != '\0')
                fprintf(stderr, "%s: Ignoring argument `%s' for option "
                        "`--%s'\n", b, t, m->name);
            __mp_optindex++;
            t = NULL;
            return m->value;
        }
    }
    /* Check that the option appears in the string of recognised options.
     */
    if ((*t == ':') || ((p = strchr(s, *t)) == NULL))
    {
        fprintf(stderr, "%s: Illegal option `-%c'\n", b, *t++);
        return '?';
    }
    r = *t++;
    /* Check to see if the option takes an argument.
     */
    if (p[1] == ':')
    {
        if (*t == '\0')
        {
            /* The rest of this argument is empty, so we proceed to the next
             * argument.
             */
            if ((++__mp_optindex >= n) || (strcmp(a[__mp_optindex], "--") == 0))
            {
                fprintf(stderr, "%s: Option `-%c' requires an argument\n", b,
                        r);
                t = NULL;
                return '?';
            }
            __mp_optarg = a[__mp_optindex];
        }
        else
            __mp_optarg = t;
        __mp_optindex++;
        t = NULL;
    }
    return r;
}
Exemplo n.º 6
0
int handle_options(int *argc, char ***argv, 
		   const struct my_option *longopts, 
		   my_bool (*get_one_option)(int,
					     const struct my_option *,
					     char *))
{
  uint opt_found, argvpos= 0, length, i;
  my_bool end_of_options= 0, must_be_var, set_maximum_value, special_used,
          option_is_loose;
  char *progname= *(*argv), **pos, **pos_end, *optend, *prev_found;
  const struct my_option *optp;
  int error;

  LINT_INIT(opt_found);
  (*argc)--; /* Skip the program name */
  (*argv)++; /*      --- || ----      */
  init_variables(longopts);

  for (pos= *argv, pos_end=pos+ *argc; pos != pos_end ; pos++)
  {
    char *cur_arg= *pos;
    if (cur_arg[0] == '-' && cur_arg[1] && !end_of_options) /* must be opt */
    {
      char *argument=    0;
      must_be_var=       0;
      set_maximum_value= 0;
      special_used=      0;
      option_is_loose=   0;

      cur_arg++;		/* skip '-' */
      if (*cur_arg == '-' || *cur_arg == 'O') /* check for long option, */
      {                                       /* --set-variable, or -O  */
	if (*cur_arg == 'O')
	{
	  must_be_var= 1;

	  if (!(*++cur_arg))	/* If not -Ovar=# */
	  {
	    /* the argument must be in next argv */
	    if (!*++pos)
	    {
	      if (my_getopt_print_errors)
		fprintf(stderr, "%s: Option '-O' requires an argument\n",
			progname);
	      return EXIT_ARGUMENT_REQUIRED;
	    }
	    cur_arg= *pos;
	    (*argc)--;
	  }
	}
	else if (!getopt_compare_strings(cur_arg, "-set-variable", 13))
	{
	  must_be_var= 1;
	  if (cur_arg[13] == '=')
	  {
	    cur_arg+= 14;
	    if (!*cur_arg)
	    {
	      if (my_getopt_print_errors)
		fprintf(stderr,
			"%s: Option '--set-variable' requires an argument\n",
			progname);
	      return EXIT_ARGUMENT_REQUIRED;
	    }
	  }
	  else if (cur_arg[14]) /* garbage, or another option. break out */
	    must_be_var= 0;
	  else
	  {
	    /* the argument must be in next argv */
	    if (!*++pos)
	    {
	      if (my_getopt_print_errors)
		fprintf(stderr,
			"%s: Option '--set-variable' requires an argument\n",
			progname);
	      return EXIT_ARGUMENT_REQUIRED;
	    }
	    cur_arg= *pos;
	    (*argc)--;
	  }
	}
	else if (!must_be_var)
	{
	  if (!*++cur_arg)	/* skip the double dash */
	  {
	    /* '--' means end of options, look no further */
	    end_of_options= 1;
	    (*argc)--;
	    continue;
	  }
	}
	optend= strcend(cur_arg, '=');
	length= optend - cur_arg;
	if (*optend == '=')
	  optend++;
	else
	  optend=0;

	/*
	  Find first the right option. Return error in case of an ambiguous,
	  or unknown option
	*/
	optp= longopts;
	if (!(opt_found= findopt(cur_arg, length, &optp, &prev_found)))
	{
	  /*
	    Didn't find any matching option. Let's see if someone called
	    option with a special option prefix
	  */
	  if (!must_be_var)
	  {
	    if (optend)
	      must_be_var= 1; /* option is followed by an argument */
	    for (i= 0; special_opt_prefix[i]; i++)
	    {
	      if (!getopt_compare_strings(special_opt_prefix[i], cur_arg,
					  special_opt_prefix_lengths[i]) &&
		  cur_arg[special_opt_prefix_lengths[i]] == '-')
	      {
		/*
		  We were called with a special prefix, we can reuse opt_found
		*/
		special_used= 1;
		cur_arg+= (special_opt_prefix_lengths[i] + 1);
		if (i == OPT_LOOSE)
		  option_is_loose= 1;
		if ((opt_found= findopt(cur_arg, length -
					(special_opt_prefix_lengths[i] + 1),
					&optp, &prev_found)))
		{
		  if (opt_found > 1)
		  {
		    if (my_getopt_print_errors)
		      fprintf(stderr,
			      "%s: ambiguous option '--%s-%s' (--%s-%s)\n",
			      progname, special_opt_prefix[i], cur_arg,
			      special_opt_prefix[i], prev_found);
		    return EXIT_AMBIGUOUS_OPTION;
		  }
		  switch (i) {
		  case OPT_SKIP:
		  case OPT_DISABLE: /* fall through */
		    /*
		      double negation is actually enable again,
		      for example: --skip-option=0 -> option = TRUE
		    */
		    optend= (optend && *optend == '0' && !(*(optend + 1))) ?
		      (char*) "1" : disabled_my_option;
		    break;
		  case OPT_ENABLE:
		    optend= (optend && *optend == '0' && !(*(optend + 1))) ?
 		      disabled_my_option : (char*) "1";
		    break;
		  case OPT_MAXIMUM:
		    set_maximum_value= 1;
		    must_be_var= 1;
		    break;
		  }
		  break; /* break from the inner loop, main loop continues */
		}
	      }
	    }
	  }
	  if (!opt_found)
	  {
	    if (must_be_var)
	    {
	      if (my_getopt_print_errors)
		fprintf(stderr,
			"%s: %s: unknown variable '%s'\n", progname,
			option_is_loose ? "WARNING" : "ERROR", cur_arg);
	      if (!option_is_loose)
		return EXIT_UNKNOWN_VARIABLE;
	    }
	    else
	    {
	      if (my_getopt_print_errors)
		fprintf(stderr,
			"%s: %s: unknown option '--%s'\n", progname,
			option_is_loose ? "WARNING" : "ERROR", cur_arg);
	      if (!option_is_loose)
		return EXIT_UNKNOWN_OPTION;
	    }
	    if (option_is_loose)
	    {
	      (*argc)--;
	      continue;
	    }
	  }
	}
	if (opt_found > 1)
	{
	  if (must_be_var)
	  {
	    if (my_getopt_print_errors)
	      fprintf(stderr, "%s: variable prefix '%s' is not unique\n",
		      progname, cur_arg);
	    return EXIT_VAR_PREFIX_NOT_UNIQUE;
	  }
	  else
	  {
	    if (my_getopt_print_errors)
	      fprintf(stderr, "%s: ambiguous option '--%s' (%s, %s)\n",
		      progname, cur_arg, prev_found, optp->name);
	    return EXIT_AMBIGUOUS_OPTION;
	  }
	}
	if (must_be_var && optp->var_type == GET_NO_ARG)
	{
	  if (my_getopt_print_errors)
	    fprintf(stderr, "%s: option '%s' cannot take an argument\n",
		    progname, optp->name);
	  return EXIT_NO_ARGUMENT_ALLOWED;
	}
	if (optp->arg_type == NO_ARG)
	{
	  if (optend && optp->var_type != GET_BOOL)
	  {
	    if (my_getopt_print_errors)
	      fprintf(stderr, "%s: option '--%s' cannot take an argument\n",
		      progname, optp->name);
	    return EXIT_NO_ARGUMENT_ALLOWED;
	  }
	  if (optp->var_type == GET_BOOL)
	  {
	    /*
	      Set bool to 1 if no argument or if the user has used
	      --enable-'option-name'.
	      *optend was set to '0' if one used --disable-option
	      */
	    *((my_bool*) optp->value)= 	(my_bool) (!optend || *optend == '1');
	    (*argc)--;	    
	    get_one_option(optp->id, optp, argument);
	    continue;
	  }
	  argument= optend;
	}
	else if (optp->arg_type == OPT_ARG && optp->var_type == GET_BOOL)
	{
	  if (optend == disabled_my_option)
	    *((my_bool*) optp->value)= (my_bool) 0;
	  else
	  {
	    if (!optend) /* No argument -> enable option */
	      *((my_bool*) optp->value)= (my_bool) 1;
	    else /* If argument differs from 0, enable option, else disable */
	      *((my_bool*) optp->value)= (my_bool) atoi(optend) != 0;
	  }
	  (*argc)--;	    
	  continue;
	}
	else if (optp->arg_type == REQUIRED_ARG && !optend)
	{
	  /* Check if there are more arguments after this one */
	  if (!*++pos)
	  {
	    if (my_getopt_print_errors)
	      fprintf(stderr, "%s: option '--%s' requires an argument\n",
		      progname, optp->name);
	    return EXIT_ARGUMENT_REQUIRED;
	  }
	  argument= *pos;
	  (*argc)--;
	}
	else
	  argument= optend;
      }
      else  /* must be short option */
      {
	for (optend= cur_arg; *optend; optend++)
	{
	  opt_found= 0;
	  for (optp= longopts; optp->id; optp++)
	  {
	    if (optp->id == (int) (uchar) *optend)
	    {
	      /* Option recognized. Find next what to do with it */
	      opt_found= 1;
	      if (optp->var_type == GET_BOOL && optp->arg_type == NO_ARG)
	      {
		*((my_bool*) optp->value)= (my_bool) 1;
		get_one_option(optp->id, optp, argument);
		continue;
	      }
	      else if (optp->arg_type == REQUIRED_ARG ||
		       optp->arg_type == OPT_ARG)
	      {
		if (*(optend + 1))
		{
		  /* The rest of the option is option argument */
		  argument= optend + 1;
		  /* This is in effect a jump out of the outer loop */
		  optend= (char*) " ";
		}
		else if (optp->arg_type == REQUIRED_ARG)
		{
		  /* Check if there are more arguments after this one */
		  if (!*++pos)
		  {
		    if (my_getopt_print_errors)
		      fprintf(stderr,
			      "%s: option '-%c' requires an argument\n",
			      progname, optp->id);
		    return EXIT_ARGUMENT_REQUIRED;
		  }
		  argument= *pos;
		  (*argc)--;
		  /* the other loop will break, because *optend + 1 == 0 */
		}
	      }
	      if ((error= setval(optp, argument, set_maximum_value)))
	      {
		fprintf(stderr,
			"%s: Error while setting value '%s' to '%s'\n",
			progname, argument, optp->name);
		return error;
	      }
	      get_one_option(optp->id, optp, argument);
	      break;
	    }
	  }
	  if (!opt_found)
	  {
	    if (my_getopt_print_errors)
	      fprintf(stderr,
		      "%s: unknown option '-%c'\n", progname, *optend);
	    return EXIT_UNKNOWN_OPTION;
	  }
	}
	(*argc)--; /* option handled (short), decrease argument count */
	continue;
      }
      if ((error= setval(optp, argument, set_maximum_value)))
      {
	fprintf(stderr,
		"%s: Error while setting value '%s' to '%s'\n",
		progname, argument, optp->name);
	return error;
      }
      get_one_option(optp->id, optp, argument);

      (*argc)--; /* option handled (short�or�long), decrease argument count */
    }
    else /* non-option found */
      (*argv)[argvpos++]= cur_arg;
  }
  /*
    Destroy the first, already handled option, so that programs that look
    for arguments in 'argv', without checking 'argc', know when to stop.
    Items in argv, before the destroyed one, are all non-option -arguments
    to the program, yet to be (possibly) handled.
  */
  (*argv)[argvpos]= 0;
  return 0;
}