Blame gettext-runtime/gnulib-lib/getopt1.c

Packit Bot 06c835
/* getopt_long and getopt_long_only entry points for GNU getopt.
Packit Bot 06c835
   Copyright (C) 1987-1994, 1996-1998, 2004, 2006, 2009-2015 Free Software
Packit Bot 06c835
   Foundation, Inc.
Packit Bot 06c835
   This file is part of the GNU C Library.
Packit Bot 06c835
Packit Bot 06c835
   This program is free software: you can redistribute it and/or modify
Packit Bot 06c835
   it under the terms of the GNU General Public License as published by
Packit Bot 06c835
   the Free Software Foundation; either version 3 of the License, or
Packit Bot 06c835
   (at your option) any later version.
Packit Bot 06c835
Packit Bot 06c835
   This program is distributed in the hope that it will be useful,
Packit Bot 06c835
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot 06c835
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Bot 06c835
   GNU General Public License for more details.
Packit Bot 06c835
Packit Bot 06c835
   You should have received a copy of the GNU General Public License
Packit Bot 06c835
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Bot 06c835

Packit Bot 06c835
#ifdef _LIBC
Packit Bot 06c835
# include <getopt.h>
Packit Bot 06c835
#else
Packit Bot 06c835
# include <config.h>
Packit Bot 06c835
# include "getopt.h"
Packit Bot 06c835
#endif
Packit Bot 06c835
#include "getopt_int.h"
Packit Bot 06c835
Packit Bot 06c835
#include <stdio.h>
Packit Bot 06c835
Packit Bot 06c835
/* This needs to come after some library #include
Packit Bot 06c835
   to get __GNU_LIBRARY__ defined.  */
Packit Bot 06c835
#ifdef __GNU_LIBRARY__
Packit Bot 06c835
#include <stdlib.h>
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
#ifndef NULL
Packit Bot 06c835
#define NULL 0
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
int
Packit Bot 06c835
getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
Packit Bot 06c835
             const struct option *long_options, int *opt_index)
Packit Bot 06c835
{
Packit Bot 06c835
  return _getopt_internal (argc, (char **) argv, options, long_options,
Packit Bot 06c835
                           opt_index, 0, 0);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
int
Packit Bot 06c835
_getopt_long_r (int argc, char **argv, const char *options,
Packit Bot 06c835
                const struct option *long_options, int *opt_index,
Packit Bot 06c835
                struct _getopt_data *d)
Packit Bot 06c835
{
Packit Bot 06c835
  return _getopt_internal_r (argc, argv, options, long_options, opt_index,
Packit Bot 06c835
                             0, d, 0);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/* Like getopt_long, but '-' as well as '--' can indicate a long option.
Packit Bot 06c835
   If an option that starts with '-' (not '--') doesn't match a long option,
Packit Bot 06c835
   but does match a short option, it is parsed as a short option
Packit Bot 06c835
   instead.  */
Packit Bot 06c835
Packit Bot 06c835
int
Packit Bot 06c835
getopt_long_only (int argc, char *__getopt_argv_const *argv,
Packit Bot 06c835
                  const char *options,
Packit Bot 06c835
                  const struct option *long_options, int *opt_index)
Packit Bot 06c835
{
Packit Bot 06c835
  return _getopt_internal (argc, (char **) argv, options, long_options,
Packit Bot 06c835
                           opt_index, 1, 0);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
int
Packit Bot 06c835
_getopt_long_only_r (int argc, char **argv, const char *options,
Packit Bot 06c835
                     const struct option *long_options, int *opt_index,
Packit Bot 06c835
                     struct _getopt_data *d)
Packit Bot 06c835
{
Packit Bot 06c835
  return _getopt_internal_r (argc, argv, options, long_options, opt_index,
Packit Bot 06c835
                             1, d, 0);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835

Packit Bot 06c835
#ifdef TEST
Packit Bot 06c835
Packit Bot 06c835
#include <stdio.h>
Packit Bot 06c835
Packit Bot 06c835
int
Packit Bot 06c835
main (int argc, char **argv)
Packit Bot 06c835
{
Packit Bot 06c835
  int c;
Packit Bot 06c835
  int digit_optind = 0;
Packit Bot 06c835
Packit Bot 06c835
  while (1)
Packit Bot 06c835
    {
Packit Bot 06c835
      int this_option_optind = optind ? optind : 1;
Packit Bot 06c835
      int option_index = 0;
Packit Bot 06c835
      static const struct option long_options[] =
Packit Bot 06c835
      {
Packit Bot 06c835
        {"add", 1, 0, 0},
Packit Bot 06c835
        {"append", 0, 0, 0},
Packit Bot 06c835
        {"delete", 1, 0, 0},
Packit Bot 06c835
        {"verbose", 0, 0, 0},
Packit Bot 06c835
        {"create", 0, 0, 0},
Packit Bot 06c835
        {"file", 1, 0, 0},
Packit Bot 06c835
        {0, 0, 0, 0}
Packit Bot 06c835
      };
Packit Bot 06c835
Packit Bot 06c835
      c = getopt_long (argc, argv, "abc:d:0123456789",
Packit Bot 06c835
                       long_options, &option_index);
Packit Bot 06c835
      if (c == -1)
Packit Bot 06c835
        break;
Packit Bot 06c835
Packit Bot 06c835
      switch (c)
Packit Bot 06c835
        {
Packit Bot 06c835
        case 0:
Packit Bot 06c835
          printf ("option %s", long_options[option_index].name);
Packit Bot 06c835
          if (optarg)
Packit Bot 06c835
            printf (" with arg %s", optarg);
Packit Bot 06c835
          printf ("\n");
Packit Bot 06c835
          break;
Packit Bot 06c835
Packit Bot 06c835
        case '0':
Packit Bot 06c835
        case '1':
Packit Bot 06c835
        case '2':
Packit Bot 06c835
        case '3':
Packit Bot 06c835
        case '4':
Packit Bot 06c835
        case '5':
Packit Bot 06c835
        case '6':
Packit Bot 06c835
        case '7':
Packit Bot 06c835
        case '8':
Packit Bot 06c835
        case '9':
Packit Bot 06c835
          if (digit_optind != 0 && digit_optind != this_option_optind)
Packit Bot 06c835
            printf ("digits occur in two different argv-elements.\n");
Packit Bot 06c835
          digit_optind = this_option_optind;
Packit Bot 06c835
          printf ("option %c\n", c);
Packit Bot 06c835
          break;
Packit Bot 06c835
Packit Bot 06c835
        case 'a':
Packit Bot 06c835
          printf ("option a\n");
Packit Bot 06c835
          break;
Packit Bot 06c835
Packit Bot 06c835
        case 'b':
Packit Bot 06c835
          printf ("option b\n");
Packit Bot 06c835
          break;
Packit Bot 06c835
Packit Bot 06c835
        case 'c':
Packit Bot 06c835
          printf ("option c with value '%s'\n", optarg);
Packit Bot 06c835
          break;
Packit Bot 06c835
Packit Bot 06c835
        case 'd':
Packit Bot 06c835
          printf ("option d with value '%s'\n", optarg);
Packit Bot 06c835
          break;
Packit Bot 06c835
Packit Bot 06c835
        case '?':
Packit Bot 06c835
          break;
Packit Bot 06c835
Packit Bot 06c835
        default:
Packit Bot 06c835
          printf ("?? getopt returned character code 0%o ??\n", c);
Packit Bot 06c835
        }
Packit Bot 06c835
    }
Packit Bot 06c835
Packit Bot 06c835
  if (optind < argc)
Packit Bot 06c835
    {
Packit Bot 06c835
      printf ("non-option ARGV-elements: ");
Packit Bot 06c835
      while (optind < argc)
Packit Bot 06c835
        printf ("%s ", argv[optind++]);
Packit Bot 06c835
      printf ("\n");
Packit Bot 06c835
    }
Packit Bot 06c835
Packit Bot 06c835
  exit (0);
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
#endif /* TEST */