aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/fish_indent.cpp
diff options
context:
space:
mode:
authorGravatar Kurtis Rader <krader@skepticism.us>2016-03-03 15:36:17 -0800
committerGravatar Kurtis Rader <krader@skepticism.us>2016-03-08 13:38:57 -0800
commit6a16bdb808b02977cc99f84b4adae420f99019d2 (patch)
tree6fcb706091da63dc24a0e1ecff84aa86e30f0e29 /src/fish_indent.cpp
parent5e09411340b347caf2f81dc65fc9d3a1fefac261 (diff)
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.
Diffstat (limited to 'src/fish_indent.cpp')
-rw-r--r--src/fish_indent.cpp47
1 files changed, 20 insertions, 27 deletions
diff --git a/src/fish_indent.cpp b/src/fish_indent.cpp
index 60ed2334..037d9b77 100644
--- a/src/fish_indent.cpp
+++ b/src/fish_indent.cpp
@@ -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>
@@ -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':
@@ -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);
}
}
}