Commit 2361280d authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

getopt: remove useless functions and boiler plate, update licens

We only use getopt_long(). Also we don't care about pre-C89 compilers.
parent b3a59c07
...@@ -429,6 +429,8 @@ SOURCES_libvlc_common = \ ...@@ -429,6 +429,8 @@ SOURCES_libvlc_common = \
config/keys.c \ config/keys.c \
config/cmdline.c \ config/cmdline.c \
config/dirs.c \ config/dirs.c \
config/getopt.c \
config/vlc_getopt.h \
misc/events.c \ misc/events.c \
misc/image.c \ misc/image.c \
misc/messages.c \ misc/messages.c \
...@@ -446,9 +448,6 @@ SOURCES_libvlc_common = \ ...@@ -446,9 +448,6 @@ SOURCES_libvlc_common = \
misc/filter_chain.c \ misc/filter_chain.c \
misc/http_auth.c \ misc/http_auth.c \
misc/sql.c \ misc/sql.c \
extras/getopt.c \
extras/getopt.h \
extras/getopt1.c \
$(NULL) $(NULL)
SOURCES_libvlc_httpd = \ SOURCES_libvlc_httpd = \
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
#include <vlc_keys.h> #include <vlc_keys.h>
#include <vlc_charset.h> #include <vlc_charset.h>
#include "../extras/getopt.h" #include "vlc_getopt.h"
#include "configuration.h" #include "configuration.h"
#include "modules/modules.h" #include "modules/modules.h"
......
/* Getopt for GNU. /*****************************************************************************
NOTE: getopt is now part of the C library, so if you don't know what * getopt_long()
"Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu *****************************************************************************
before changing it! * Copyright (C) 1987-1997 Free Software Foundation, Inc.
* Copyright (C) 2005-2010 the VideoLAN team
Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97 *
Free Software Foundation, Inc. * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
This file is part of the GNU C Library. Its master source is NOT part of * the Free Software Foundation; either version 2.1 of the License, or
the C library, however. The master source lives in /gd/gnu/lib. * (at your option) any later version.
*
The GNU C Library is free software; you can redistribute it and/or * This program is distributed in the hope that it will be useful,
modify it under the terms of the GNU Library General Public License as * but WITHOUT ANY WARRANTY; without even the implied warranty of
published by the Free Software Foundation; either version 2 of the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
License, or (at your option) any later version. * GNU General Public License for more details.
*
The GNU C Library is distributed in the hope that it will be useful, * You should have received a copy of the GNU Lesser General Public License
but WITHOUT ANY WARRANTY; without even the implied warranty of * along with this program; if not, write to the Free Software Foundation,
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Library General Public License for more details. *****************************************************************************/
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
Ditto for AIX 3.2 and <stdlib.h>. */
#ifndef _NO_PROTO
#define _NO_PROTO
#endif
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include <config.h> #include <config.h>
#endif #endif
#include <vlc_common.h> #include <vlc_common.h>
#if !defined (__STDC__) || !__STDC__
/* This is a separate conditional since some stdc systems
reject `defined (const)'. */
#ifndef const
#define const
#endif
#endif
#include <stdio.h> #include <stdio.h>
/* Comment out all this code if we are using the GNU C Library, and are not
actually compiling the library itself. This code is part of the GNU C
Library, but also included in many other GNU distributions. Compiling
and linking in this code is a waste when using the GNU C library
(especially if it is a shared library). Rather than having every GNU
program understand `configure --with-gnu-libc' and omit the object files,
it is simpler to just do this in the source for each such file. */
#define GETOPT_INTERFACE_VERSION 2
#if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2
#include <gnu-versions.h>
#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
#define ELIDE_CODE
#endif
#endif
/* This needs to come after some library #include
to get __GNU_LIBRARY__ defined. */
#ifdef __GNU_LIBRARY__
/* Don't include stdlib.h for non-GNU C libraries because some of them
contain conflicting prototypes for getopt. */
#include <stdlib.h>
#include <unistd.h>
#endif /* GNU C library. */
#ifdef VMS
#include <unixlib.h>
#if HAVE_STRING_H - 0
#include <string.h> #include <string.h>
#ifdef STRNCASECMP_IN_STRINGS_H
# include <strings.h>
#endif
#endif
#endif
#if defined (WIN32) && !defined (__CYGWIN32__) || defined(UNDER_CE)
/* It's not Unix, really. See? Capital letters. */
#include <windows.h>
#define getpid() GetCurrentProcessId()
#endif
#ifndef _
/* This is for other GNU distributions with internationalized messages.
When compiling libc, the _ macro is predefined. */
#ifdef HAVE_LIBINTL_H
#include <libintl.h>
#define _(msgid) vlc_gettext (msgid)
#else
#define _(msgid) (msgid)
#endif
#endif
/* This version of `getopt' appears to the caller like standard Unix `getopt' /* This version of `getopt' appears to the caller like standard Unix `getopt'
but it behaves differently for the user, since it allows the user but it behaves differently for the user, since it allows the user
...@@ -111,7 +41,7 @@ ...@@ -111,7 +41,7 @@
GNU application programs can use a third alternative mode in which GNU application programs can use a third alternative mode in which
they can distinguish the relative order of options and other arguments. */ they can distinguish the relative order of options and other arguments. */
#include "getopt.h" #include "vlc_getopt.h"
/* For communication from `getopt' to the caller. /* For communication from `getopt' to the caller.
When `getopt' finds an option that takes an argument, When `getopt' finds an option that takes an argument,
...@@ -199,48 +129,7 @@ ordering; ...@@ -199,48 +129,7 @@ ordering;
/* Value of POSIXLY_CORRECT environment variable. */ /* Value of POSIXLY_CORRECT environment variable. */
static char *posixly_correct; static char *posixly_correct;
#ifdef __GNU_LIBRARY__
/* We want to avoid inclusion of string.h with non-GNU libraries
because there are many ways it can cause trouble.
On some systems, it contains special magic macros that don't work
in GCC. */
#include <string.h>
#define my_index strchr
#else
/* Avoid depending on library functions or files
whose names are inconsistent. */
static char *
my_index(str, chr)
const char *str;
int chr;
{
while (*str)
{
if (*str == chr)
return (char *) str;
str++;
}
return 0;
}
/* If using GCC, we can safely declare strlen this way.
If not using GCC, it is ok not to declare it. */
#ifdef __GNUC__
/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
That was relevant to code that was here before. */
#if !defined (__STDC__) || !__STDC__
/* gcc with -traditional declares the built-in strlen to return int,
and has done so at least since version 2.4.5. -- rms. */
extern int strlen(const char *);
#endif /* not __STDC__ */
#endif /* __GNUC__ */
#endif /* not __GNU_LIBRARY__ */
/* Handle permutation of arguments. */ /* Handle permutation of arguments. */
/* Describe the part of ARGV that contains non-options that have /* Describe the part of ARGV that contains non-options that have
...@@ -250,31 +139,6 @@ extern int strlen(const char *); ...@@ -250,31 +139,6 @@ extern int strlen(const char *);
static int first_nonopt; static int first_nonopt;
static int last_nonopt; static int last_nonopt;
#ifdef _LIBC
/* Bash 2.0 gives us an environment variable containing flags
indicating ARGV elements that should not be considered arguments. */
static const char *nonoption_flags;
static int nonoption_flags_len;
static int original_argc;
static char *const *original_argv;
/* Make sure the environment variable bash 2.0 puts in the environment
is valid for the getopt call we must make sure that the ARGV passed
to getopt is that one passed to the process. */
static void store_args(int argc, char *const *argv) __attribute__((unused));
static void
store_args(int argc, char *const *argv)
{
/* XXX This is no good solution. We should rather copy the args so
that we can compare them later. But we must not use malloc(3). */
original_argc = argc;
original_argv = argv;
}
text_set_element(__libc_subinit, store_args);
#endif
/* Exchange two adjacent subsequences of ARGV. /* Exchange two adjacent subsequences of ARGV.
One subsequence is elements [first_nonopt,last_nonopt) One subsequence is elements [first_nonopt,last_nonopt)
which contains all the non-options that have been skipped so far. which contains all the non-options that have been skipped so far.
...@@ -284,11 +148,8 @@ text_set_element(__libc_subinit, store_args); ...@@ -284,11 +148,8 @@ text_set_element(__libc_subinit, store_args);
`first_nonopt' and `last_nonopt' are relocated so that they describe `first_nonopt' and `last_nonopt' are relocated so that they describe
the new indices of the non-options in ARGV after they are moved. */ the new indices of the non-options in ARGV after they are moved. */
#if defined (__STDC__) && __STDC__
static void exchange(char **); static void exchange(char **);
#endif
static void static void
exchange(argv) exchange(argv)
char **argv; char **argv;
...@@ -347,10 +208,8 @@ static void ...@@ -347,10 +208,8 @@ static void
/* Initialize the internal data when the first call is made. */ /* Initialize the internal data when the first call is made. */
#if defined (__STDC__) && __STDC__
static const char *_getopt_initialize(int, char *const *, const char *); static const char *_getopt_initialize(int, char *const *, const char *);
#endif
static const char * static const char *
_getopt_initialize(argc, argv, optstring) _getopt_initialize(argc, argv, optstring)
int argc; int argc;
...@@ -386,27 +245,6 @@ static const char * ...@@ -386,27 +245,6 @@ static const char *
else else
ordering = PERMUTE; ordering = PERMUTE;
#ifdef _LIBC
if (posixly_correct == NULL
&& argc == original_argc && argv == original_argv)
{
/* Bash 2.0 puts a special variable in the environment for each
command it runs, specifying which ARGV elements are the results of
file name wildcard expansion and therefore should not be
considered as options. */
char var[100];
sprintf(var, "_%d_GNU_nonoption_argv_flags_", getpid());
nonoption_flags = getenv(var);
if (nonoption_flags == NULL)
nonoption_flags_len = 0;
else
nonoption_flags_len = strlen(nonoption_flags);
}
else
nonoption_flags_len = 0;
#endif
return optstring; return optstring;
} }
...@@ -461,19 +299,15 @@ static const char * ...@@ -461,19 +299,15 @@ static const char *
LONGIND returns the index in LONGOPT of the long-named option found. LONGIND returns the index in LONGOPT of the long-named option found.
It is only valid when a long-named option has been found by the most It is only valid when a long-named option has been found by the most
recent call. recent call. */
If LONG_ONLY is nonzero, '-' as well as '--' can introduce
long-named options. */
int int
_getopt_internal(argc, argv, optstring, longopts, longind, long_only) vlc_getopt_long(argc, argv, optstring, longopts, longind)
int argc; int argc;
char *const *argv; char *const *argv;
const char *optstring; const char *optstring;
const struct option *longopts; const struct option *longopts;
int *longind; int *longind;
int long_only;
{ {
optarg = NULL; optarg = NULL;
...@@ -484,17 +318,7 @@ int ...@@ -484,17 +318,7 @@ int
__getopt_initialized = 1; __getopt_initialized = 1;
} }
/* Test whether ARGV[optind] points to a non-option argument.
Either it does not have option syntax, or there is an environment flag
from the shell indicating it is not an option. The later information
is only used when the used in the GNU libc. */
#ifdef _LIBC
#define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
|| (optind < nonoption_flags_len \
&& nonoption_flags[optind] == '1'))
#else
#define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
#endif
if (nextchar == NULL || *nextchar == '\0') if (nextchar == NULL || *nextchar == '\0')
{ {
...@@ -570,27 +394,14 @@ int ...@@ -570,27 +394,14 @@ int
Skip the initial punctuation. */ Skip the initial punctuation. */
nextchar = (argv[optind] + 1 nextchar = (argv[optind] + 1
+ (longopts != NULL && argv[optind][1] == '-')); + (argv[optind][1] == '-'));
} }
/* Decode the current option-ARGV-element. */ /* Decode the current option-ARGV-element. */
/* Check whether the ARGV-element is a long option. /* Check whether the ARGV-element is a long option. */
If long_only and the ARGV-element has the form "-f", where f is
a valid short option, don't consider it an abbreviated form of
a long option that starts with f. Otherwise there would be no
way to give the -f short option.
On the other hand, if there's a long option "fubar" and
the ARGV-element is "-fu", do consider that an abbreviation of
the long option, just like "--fu", and not "-f" with arg "u".
This distinction seems to be the most useful approach. */ if (argv[optind][1] == '-')
if (longopts != NULL
&& (argv[optind][1] == '-'
|| (long_only && (argv[optind][2] || !my_index(optstring, argv[optind][1])))))
{ {
char *nameend; char *nameend;
const struct option *p; const struct option *p;
...@@ -697,39 +508,32 @@ int ...@@ -697,39 +508,32 @@ int
return pfound->val; return pfound->val;
} }
/* Can't find it as a long option. If this is not getopt_long_only, /* Can't find it as a long option. It's an error. */
or the option starts with '--' or is not a valid short if (opterr)
option, then it's an error.
Otherwise interpret it as a short option. */
if (!long_only || argv[optind][1] == '-'
|| my_index(optstring, *nextchar) == NULL)
{ {
if (opterr) if (argv[optind][1] == '-')
/* --option */
fprintf(stderr, _("%s: unrecognized option `%s%s'\n"),
"--", argv[0], nextchar);
else
{ {
if (argv[optind][1] == '-') char t[2] = { argv[optind][0], '\0' };
/* --option */ /* +option or -option */
fprintf(stderr, _("%s: unrecognized option `%s%s'\n"), fprintf(stderr, _("%s: unrecognized option `%s%s'\n"),
"--", argv[0], nextchar); argv[0], t, nextchar);
else
{
char t[2] = { argv[optind][0], '\0' };
/* +option or -option */
fprintf(stderr, _("%s: unrecognized option `%s%s'\n"),
argv[0], t, nextchar);
}
} }
nextchar = (char *) "";
optind++;
optopt = 0;
return '?';
} }
nextchar = (char *) "";
optind++;
optopt = 0;
return '?';
} }
/* Look at and handle the next short option-character. */ /* Look at and handle the next short option-character. */
{ {
char c = *nextchar++; char c = *nextchar++;
char *temp = my_index(optstring, c); char *temp = strchr(optstring, c);
/* Increment `optind' when we start to process its last character. */ /* Increment `optind' when we start to process its last character. */
if (*nextchar == '\0') if (*nextchar == '\0')
...@@ -923,87 +727,3 @@ int ...@@ -923,87 +727,3 @@ int
return c; return c;
} }
} }
int
getopt(argc, argv, optstring)
int argc;
char *const *argv;
const char *optstring;
{
return _getopt_internal(argc, argv, optstring,
(const struct option *) 0,
(int *) 0,
0);
}
#ifdef TEST
/* Compile with -DTEST to make an executable for use in testing
the above definition of `getopt'. */
int
main(argc, argv)
int argc;
char **argv;
{
int c;
int digit_optind = 0;
while (1)
{
int this_option_optind = optind ? optind : 1;
c = getopt(argc, argv, "abc:d:0123456789");
if (c == -1)
break;
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf("option %c\n", c);
break;
case 'a':
printf("option a\n");
break;
case 'b':
printf("option b\n");
break;
case 'c':
printf("option c with value `%s'\n", optarg);
break;
case '?':
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc)
{
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
exit(0);
}
#endif /* TEST */
/* Declarations for getopt. /*****************************************************************************
Copyright (C) 1989,90,91,92,93,94,96,97 Free Software Foundation, Inc. * Declarations for getopt_long()
*****************************************************************************
This file is part of the GNU C Library. Its master source is NOT part of * Copyright (C) 1987-1997 Free Software Foundation, Inc.
the C library, however. The master source lives in /gd/gnu/lib. * Copyright (C) 2005-2010 the VideoLAN team
*
The GNU C Library is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or modify
modify it under the terms of the GNU Library General Public License as * it under the terms of the GNU Lesser General Public License as published by
published by the Free Software Foundation; either version 2 of the * the Free Software Foundation; either version 2.1 of the License, or
License, or (at your option) any later version. * (at your option) any later version.
*
The GNU C Library is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Library General Public License for more details. * GNU General Public License for more details.
*
You should have received a copy of the GNU Library General Public * You should have received a copy of the GNU Lesser General Public License
License along with the GNU C Library; see the file COPYING.LIB. If not, * along with this program; if not, write to the Free Software Foundation,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Boston, MA 02111-1307, USA. */ *****************************************************************************/
#ifndef _GETOPT_H #ifndef VLC_GETOPT_H
#define _GETOPT_H 1 #define VLC_GETOPT_H 1
#ifdef __cplusplus
extern "C"
{
#endif
/* For communication from `getopt' to the caller. /* For communication from `getopt' to the caller.
When `getopt' finds an option that takes an argument, When `getopt' finds an option that takes an argument,
...@@ -81,11 +76,7 @@ extern "C" ...@@ -81,11 +76,7 @@ extern "C"
struct option struct option
{ {
#if defined (__STDC__) && __STDC__
const char *name; const char *name;
#else
char *name;
#endif
/* has_arg can't be an enum because some compilers complain about /* has_arg can't be an enum because some compilers complain about
type mismatches in all the code that assumes it is an int. */ type mismatches in all the code that assumes it is an int. */
int has_arg; int has_arg;
...@@ -99,36 +90,7 @@ extern "C" ...@@ -99,36 +90,7 @@ extern "C"
#define required_argument 1 #define required_argument 1
#define optional_argument 2 #define optional_argument 2
#if defined (__STDC__) && __STDC__
#ifdef __GNU_LIBRARY__
/* Many other libraries have conflicting prototypes for getopt, with
differences in the consts, in stdlib.h. To avoid compilation
errors, only prototype getopt for the GNU C library. */
extern int getopt(int argc, char *const *argv, const char *shortopts);
#else /* not __GNU_LIBRARY__ */
extern int getopt();
#endif /* __GNU_LIBRARY__ */
extern int vlc_getopt_long(int argc, char *const *argv, const char *shortopts, extern int vlc_getopt_long(int argc, char *const *argv, const char *shortopts,
const struct option *longopts, int *longind); const struct option *longopts, int *longind);
extern int getopt_long_only(int argc, char *const *argv,
const char *shortopts,
const struct option *longopts, int *longind);
/* Internal only. Users should not call this directly. */
extern int _getopt_internal(int argc, char *const *argv,
const char *shortopts,
const struct option *longopts, int *longind,
int long_only);
#else /* not __STDC__ */
extern int getopt();
extern int vlc_getopt_long();
extern int getopt_long_only();
extern int _getopt_internal();
#endif /* __STDC__ */
#ifdef __cplusplus
}
#endif
#endif /* _GETOPT_H */ #endif /* VLC_GETOPT_H */
/* getopt_long and getopt_long_only entry points for GNU getopt.
Copyright (C) 1987,88,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
This file is part of the GNU C Library. Its master source is NOT part of
the C library, however. The master source lives in /gd/gnu/lib.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,