assume getopt/getopt_long is available

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 #2790.
This commit is contained in:
Kurtis Rader
2016-03-03 15:36:17 -08:00
parent 5e09411340
commit 6a16bdb808
6 changed files with 27 additions and 150 deletions

View File

@@ -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>
@@ -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"
@@ -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;
@@ -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' },
@@ -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':
@@ -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);
}