Skip to content

Commit

Permalink
assume getopt/getopt_long is available
Browse files Browse the repository at this point in the history
There is no longer a good reason to detect whether or not getopt_long()
is available. All UNIX implementations we're likely to run on have it. And
if we ever find one that doesn't the right thing to do is not fallback to
getopt() but to include the getopt_long() source in our package like we
do with the pcre2 library. Since it's licensed under LGPL we can legally
do so if it becomes necessary.

This partially addresses issue fish-shell#2790.
  • Loading branch information
krader1961 authored and floam committed Mar 15, 2016
1 parent 0825c94 commit 3e282aa
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 151 deletions.
37 changes: 0 additions & 37 deletions configure.ac
Expand Up @@ -752,43 +752,6 @@ else
AC_MSG_RESULT(no)
fi

# Check if getopt_long exists and works
AC_MSG_CHECKING([if getopt_long exists and works])
AC_TRY_LINK(
[
#if HAVE_GETOPT_H
#include <getopt.h>
#endif
],
[
static struct option
long_options[] =
{
0, 0, 0, 0
}
;
int opt = getopt_long( 0,
0,
0,
long_options,
0 );
],
have_working_getopt_long=yes,
have_working_getopt_long=no
)
if test "$have_working_getopt_long" = yes; then
AC_MSG_RESULT(yes)
AC_DEFINE(
[HAVE_WORKING_GETOPT_LONG],
[1],
[Define to 1 if getopt_long exists and works.]
)
else
AC_MSG_RESULT(no)
fi


# Check for Solaris curses tputs having fixed length parameter list.
AC_MSG_CHECKING([if we are using non varargs tparm.])
AC_COMPILE_IFELSE(
Expand Down
14 changes: 0 additions & 14 deletions src/fallback.cpp
Expand Up @@ -1207,20 +1207,6 @@ int killpg(int pgr, int sig)
}
#endif

#ifndef HAVE_WORKING_GETOPT_LONG

int getopt_long(int argc,
char * const argv[],
const char *optstring,
const struct option *longopts,
int *longindex)
{
return getopt(argc, argv, optstring);
}


#endif

#ifndef HAVE_BACKTRACE
int backtrace(void **buffer, int size)
{
Expand Down
47 changes: 0 additions & 47 deletions src/fallback.h
Expand Up @@ -413,53 +413,6 @@ extern int _nl_msg_cat_cntr;
int killpg(int pgr, int sig);
#endif


#ifndef HAVE_WORKING_GETOPT_LONG

/**
Struct describing a long getopt option
*/
struct option
{
/**
Name of option
*/
const char *name;
/**
Flag
*/
int has_arg;
/**
Flag
*/
int *flag;
/**
Return value
*/
int val;
}
;

#ifndef no_argument
#define no_argument 0
#endif

#ifndef required_argument
#define required_argument 1
#endif

#ifndef optional_argument
#define optional_argument 2
#endif

int getopt_long(int argc,
char * const argv[],
const char *optstring,
const struct option *longopts,
int *longindex);

#endif

#ifndef HAVE_SYSCONF

#define _SC_ARG_MAX 1
Expand Down
26 changes: 8 additions & 18 deletions src/fish.cpp
Expand Up @@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include "config.h"

#include <assert.h>
#include <getopt.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
Expand All @@ -39,15 +40,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include <sys/socket.h> // IWYU pragma: keep - suggests internal header
#include <sys/un.h>
#include <pwd.h>

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

#include <locale.h>

#include "fallback.h" // IWYU pragma: keep

#include "common.h"
#include "reader.h"
#include "builtin.h"
Expand All @@ -72,11 +67,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#define PATH_MAX 1024
#endif

/**
The string describing the single-character options accepted by the main fish binary
*/
#define GETOPT_STRING "+hilnvc:p:d:"

/* If we are doing profiling, the filename to output to */
static const char *s_profiling_output_filename = NULL;

Expand Down Expand Up @@ -370,7 +360,8 @@ static int read_init(const struct config_paths_t &paths)
*/
static int fish_parse_opt(int argc, char **argv, std::vector<std::string> *cmds)
{
const struct option long_options[] =
const char *short_opts = "+hilnvc:p:d:";
const struct option long_opts[] =
{
{ "command", required_argument, NULL, 'c' },
{ "debug-level", required_argument, NULL, 'd' },
Expand All @@ -383,17 +374,15 @@ static int fish_parse_opt(int argc, char **argv, std::vector<std::string> *cmds)
{ NULL, 0, NULL, 0 }
};

while (1)
int opt;
while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1)
{
int opt = getopt_long(argc, argv, GETOPT_STRING, long_options, NULL);
if (opt == -1)
break;

switch (opt)
{
case 0:
{
break;
fwprintf(stderr, _(L"getopt_long() unexpectedly returned zero\n"));
exit_without_destructors(127);
}

case 'c':
Expand Down Expand Up @@ -462,6 +451,7 @@ static int fish_parse_opt(int argc, char **argv, std::vector<std::string> *cmds)

default:
{
// We assume getopt_long() has already emitted a diagnostic msg.
exit_without_destructors(1);
}

Expand Down
47 changes: 20 additions & 27 deletions src/fish_indent.cpp
Expand Up @@ -21,13 +21,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

#include "config.h"

#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <vector>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <assert.h>
#include <locale.h>
#include <stddef.h>
Expand Down Expand Up @@ -297,40 +295,34 @@ int main(int argc, char *argv[])
output_type_ansi,
output_type_html
} output_type = output_type_plain_text;

/* Whether to indent (true) or just reformat to one job per line (false) */
bool do_indent = true;

while (1)
const char *short_opts = "+hvi";
const struct option long_opts[] =
{
{ "no-indent", no_argument, NULL, 'i' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{ "html", no_argument, NULL, 1 },
{ "ansi", no_argument, NULL, 2 },
{ NULL, 0, NULL, 0 }
};

int opt;
while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1)
{
const struct option long_options[] =
{
{ "no-indent", no_argument, 0, 'i' },
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ "html", no_argument, 0, 1 },
{ "ansi", no_argument, 0, 2 },
{ 0, 0, 0, 0 }
};

int opt_index = 0;
int opt = getopt_long(argc, argv, "hvi", long_options, &opt_index);
if (opt == -1)
break;

switch (opt)
{
case 0:
{
break;
fwprintf(stderr, _(L"getopt_long() unexpectedly returned zero\n"));
exit_without_destructors(127);
}

case 'h':
{
print_help("fish_indent", 1);
exit(0);
assert(0 && "Unreachable code reached");
break;
exit_without_destructors(0);
}

case 'v':
Expand Down Expand Up @@ -359,9 +351,10 @@ int main(int argc, char *argv[])
break;
}

case '?':
default:
{
exit(1);
// We assume getopt_long() has already emitted a diagnostic msg.
exit_without_destructors(1);
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/fish_tests.cpp
Expand Up @@ -4,7 +4,6 @@

#include "config.h"


#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
Expand All @@ -24,20 +23,13 @@
#include <sstream>
#include <algorithm>
#include <iterator>

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

#include <signal.h>

#include <locale.h>
#include <dirent.h>
#include <time.h>

#include "fallback.h"
#include "util.h"

#include "common.h"
#include "proc.h"
#include "reader.h"
Expand Down

0 comments on commit 3e282aa

Please sign in to comment.