Skip to content

Commit

Permalink
Sync from GLIBC.
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoldrobbins committed Mar 16, 2012
1 parent 58b5aa3 commit a19298e
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 91 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2012-03-16 Arnold D. Robbins <arnold@skeeve.com>

* getopt.c, getopt.h, getopt1.c, getopt_int.h, regcomp.c,
regex.c, regex.h, regex_internal.c, regex_internal.h,
regexec.c: Sync with GLIBC, what the heck.

2012-03-14 Eli Zaretskii <eliz@gnu.org>

* mbsupport.h (btowc): Change for non-DJGPP.
Expand Down
118 changes: 85 additions & 33 deletions getopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
NOTE: getopt is part of the C library, so if you don't know what
"Keep this file name-space clean" means, talk to drepper@gnu.org
before changing it!
Copyright (C) 1987-1996,1998-2004,2008,2009,2010
Copyright (C) 1987-1996,1998-2004,2008,2009,2010,2011
Free Software Foundation, Inc.
This file is part of the GNU C Library.
Expand All @@ -17,9 +17,8 @@
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA. */
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */

/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
Ditto for AIX 3.2 and <stdlib.h>. */
Expand Down Expand Up @@ -74,7 +73,7 @@
# define _(msgid) gettext (msgid)
#endif

#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
# include <wchar.h>
#endif

Expand Down Expand Up @@ -526,23 +525,29 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
|| !strchr (optstring, argv[d->optind][1])))))
{
char *nameend;
unsigned int namelen;
const struct option *p;
const struct option *pfound = NULL;
struct option_list
{
const struct option *p;
struct option_list *next;
int needs_free;
} *ambig_list = NULL;
int exact = 0;
int ambig = 0;
int indfound = -1;
int option_index;

for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
/* Do nothing. */ ;
namelen = nameend - d->__nextchar;

/* Test all long options for either exact match
or abbreviated matches. */
for (p = longopts, option_index = 0; p->name; p++, option_index++)
if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
if (!strncmp (p->name, d->__nextchar, namelen))
{
if ((unsigned int) (nameend - d->__nextchar)
== (unsigned int) strlen (p->name))
if (namelen == (unsigned int) strlen (p->name))
{
/* Exact match found. */
pfound = p;
Expand All @@ -560,19 +565,46 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
|| pfound->has_arg != p->has_arg
|| pfound->flag != p->flag
|| pfound->val != p->val)
{
/* Second or later nonexact match found. */
ambig = 1;
struct option_list *newp = malloc (sizeof (*newp));
newp->p = p;
newp->needs_free = 1;
newp->next = ambig_list;
ambig_list = newp;
}
}

if (ambig && !exact)
if (ambig_list != NULL && !exact)
{
if (print_errors)
{
#if defined _LIBC && defined USE_IN_LIBIO
char *buf;
struct option_list first;
first.p = pfound;
first.next = ambig_list;
ambig_list = &first;

if (__asprintf (&buf, _("%s: option '%s' is ambiguous\n"),
argv[0], argv[d->optind]) >= 0)
#if defined _LIBC
char *buf = NULL;
size_t buflen = 0;

FILE *fp = open_memstream (&buf, &buflen);
if (fp != NULL)
{
fprintf (fp,
_("%s: option '%s' is ambiguous; possibilities:"),
argv[0], argv[d->optind]);

do
{
fprintf (fp, " '--%s'", ambig_list->p->name);
ambig_list = ambig_list->next;
}
while (ambig_list != NULL);

fputc_unlocked ('\n', fp);

if (__builtin_expect (fclose (fp) != EOF, 1))
{
_IO_flockfile (stderr);

Expand All @@ -586,9 +618,24 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,

free (buf);
}
}
#else
fprintf (stderr, _("%s: option '%s' is ambiguous\n"),
fprintf (stderr,
_("%s: option '%s' is ambiguous; possibilities:"),
argv[0], argv[d->optind]);
do
{
struct option_list *tmp_next;

fprintf (stderr, " '--%s'", ambig_list->p->name);
tmp_next = ambig_list->next;
if (ambig_list->needs_free)
free(ambig_list);
ambig_list = tmp_next;
}
while (ambig_list != NULL);

fputc ('\n', stderr);
#endif
}
d->__nextchar += strlen (d->__nextchar);
Expand All @@ -611,15 +658,15 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
{
if (print_errors)
{
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
char *buf;
int n;
#endif

if (argv[d->optind - 1][1] == '-')
{
/* --option */
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
n = __asprintf (&buf, _("\
%s: option '--%s' doesn't allow an argument\n"),
argv[0], pfound->name);
Expand All @@ -632,7 +679,7 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
else
{
/* +option or -option */
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
n = __asprintf (&buf, _("\
%s: option '%c%s' doesn't allow an argument\n"),
argv[0], argv[d->optind - 1][0],
Expand All @@ -645,7 +692,7 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
#endif
}

#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
if (n >= 0)
{
_IO_flockfile (stderr);
Expand Down Expand Up @@ -678,7 +725,7 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
{
if (print_errors)
{
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
char *buf;

if (__asprintf (&buf, _("\
Expand Down Expand Up @@ -729,15 +776,15 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
{
if (print_errors)
{
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
char *buf;
int n;
#endif

if (argv[d->optind][1] == '-')
{
/* --option */
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
argv[0], d->__nextchar);
#else
Expand All @@ -748,7 +795,7 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
else
{
/* +option or -option */
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
argv[0], argv[d->optind][0], d->__nextchar);
#else
Expand All @@ -757,7 +804,7 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
#endif
}

#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
if (n >= 0)
{
_IO_flockfile (stderr);
Expand Down Expand Up @@ -795,19 +842,19 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
{
if (print_errors)
{
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
char *buf;
int n;
#endif

#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"),
argv[0], c);
#else
fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
#endif

#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
if (n >= 0)
{
_IO_flockfile (stderr);
Expand All @@ -830,6 +877,9 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
/* Convenience. Treat POSIX -W foo same as long option --foo */
if (temp[0] == 'W' && temp[1] == ';')
{
if (longopts == NULL)
goto no_longs;

char *nameend;
const struct option *p;
const struct option *pfound = NULL;
Expand All @@ -850,7 +900,7 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
{
if (print_errors)
{
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
char *buf;

if (__asprintf (&buf,
Expand Down Expand Up @@ -924,7 +974,7 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
{
if (print_errors)
{
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
char *buf;

if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
Expand Down Expand Up @@ -964,7 +1014,7 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
{
if (print_errors)
{
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
char *buf;

if (__asprintf (&buf, _("\
Expand Down Expand Up @@ -1003,7 +1053,7 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
{
if (print_errors)
{
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
char *buf;

if (__asprintf (&buf, _("\
Expand Down Expand Up @@ -1045,6 +1095,8 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
}
return pfound->val;
}

no_longs:
d->__nextchar = NULL;
return 'W'; /* Let the application handle it. */
}
Expand Down Expand Up @@ -1076,7 +1128,7 @@ _getopt_internal_r (int argc, char *const *argv, const char *optstring,
{
if (print_errors)
{
#if defined _LIBC && defined USE_IN_LIBIO
#if defined _LIBC
char *buf;

if (__asprintf (&buf, _("\
Expand Down
11 changes: 5 additions & 6 deletions getopt.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Declarations for getopt.
Copyright (C) 1989-1994,1996-1999,2001,2003,2004
Copyright (C) 1989-1994,1996-1999,2001,2003,2004,2009,2010
Free Software Foundation, Inc.
This file is part of the GNU C Library.
Expand All @@ -14,9 +14,8 @@
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA. */
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */

#ifndef _GETOPT_H

Expand Down Expand Up @@ -158,9 +157,9 @@ extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
additional functionality can be disable at runtime. This redirection
helps to also do this at runtime. */
# ifdef __REDIRECT
extern int __REDIRECT (getopt, (int ___argc, char *const *___argv,
extern int __REDIRECT_NTH (getopt, (int ___argc, char *const *___argv,
const char *__shortopts),
__posix_getopt) __THROW;
__posix_getopt);
# else
extern int __posix_getopt (int ___argc, char *const *___argv,
const char *__shortopts) __THROW;
Expand Down
5 changes: 2 additions & 3 deletions getopt1.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA. */
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
#include <config.h>
Expand Down
5 changes: 2 additions & 3 deletions getopt_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA. */
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */

#ifndef _GETOPT_INT_H
#define _GETOPT_INT_H 1
Expand Down
Loading

0 comments on commit a19298e

Please sign in to comment.