提交 91184c4f 编写于 作者: R Rich Felker

add error message printing to getopt_long and make related improvements

some related changes are also made to getopt, and the return value of
getopt_long in the case of missing arguments is fixed.
上级 d3357268
...@@ -12,7 +12,7 @@ int optind=1, opterr=1, optopt, __optpos, __optreset=0; ...@@ -12,7 +12,7 @@ int optind=1, opterr=1, optopt, __optpos, __optreset=0;
#define optpos __optpos #define optpos __optpos
weak_alias(__optreset, optreset); weak_alias(__optreset, optreset);
void __getopt_msg(const char *a, const char *b, const char *c, int l) void __getopt_msg(const char *a, const char *b, const char *c, size_t l)
{ {
FILE *f = stderr; FILE *f = stderr;
b = __lctrans_cur(b); b = __lctrans_cur(b);
...@@ -80,7 +80,7 @@ int getopt(int argc, char * const argv[], const char *optstring) ...@@ -80,7 +80,7 @@ int getopt(int argc, char * const argv[], const char *optstring)
if (d != c) { if (d != c) {
if (optstring[0] != ':' && opterr) if (optstring[0] != ':' && opterr)
__getopt_msg(argv[0], ": illegal option: ", optchar, k); __getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
return '?'; return '?';
} }
if (optstring[i] == ':') { if (optstring[i] == ':') {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <stddef.h> #include <stddef.h>
#include <getopt.h> #include <getopt.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
extern int __optpos, __optreset; extern int __optpos, __optreset;
...@@ -15,11 +16,14 @@ static void permute(char *const *argv, int dest, int src) ...@@ -15,11 +16,14 @@ static void permute(char *const *argv, int dest, int src)
av[dest] = tmp; av[dest] = tmp;
} }
void __getopt_msg(const char *, const char *, const char *, size_t);
static int __getopt_long_core(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly); static int __getopt_long_core(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly);
static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly) static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
{ {
int ret, skipped, resumed; int ret, skipped, resumed;
const char *optstring2 = optstring + 1;
if (!optind || __optreset) { if (!optind || __optreset) {
__optreset = 0; __optreset = 0;
__optpos = 0; __optpos = 0;
...@@ -34,9 +38,10 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con ...@@ -34,9 +38,10 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con
if (argv[i][0] == '-' && argv[i][1]) break; if (argv[i][0] == '-' && argv[i][1]) break;
} }
optind = i; optind = i;
optstring2 = optstring;
} }
resumed = optind; resumed = optind;
ret = __getopt_long_core(argc, argv, optstring, longopts, idx, longonly); ret = __getopt_long_core(argc, argv, optstring2, longopts, idx, longonly);
if (resumed > skipped) { if (resumed > skipped) {
int i, cnt = optind-resumed; int i, cnt = optind-resumed;
for (i=0; i<cnt; i++) for (i=0; i<cnt; i++)
...@@ -72,12 +77,27 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring ...@@ -72,12 +77,27 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
i = match; i = match;
optind++; optind++;
if (*opt == '=') { if (*opt == '=') {
if (!longopts[i].has_arg) return '?'; if (!longopts[i].has_arg) {
if (optstring[0] == ':' || !opterr)
return '?';
__getopt_msg(argv[0],
": option does not take an argument: ",
longopts[i].name,
strlen(longopts[i].name));
return '?';
}
optarg = opt+1; optarg = opt+1;
} else { } else {
if (longopts[i].has_arg == required_argument) { if (longopts[i].has_arg == required_argument) {
if (!(optarg = argv[optind])) if (!(optarg = argv[optind])) {
return ':'; if (optstring[0] == ':' || !opterr)
return ':';
__getopt_msg(argv[0],
": option requires an argument: ",
longopts[i].name,
strlen(longopts[i].name));
return '?';
}
optind++; optind++;
} else optarg = NULL; } else optarg = NULL;
} }
...@@ -89,6 +109,12 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring ...@@ -89,6 +109,12 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
return longopts[i].val; return longopts[i].val;
} }
if (argv[optind][1] == '-') { if (argv[optind][1] == '-') {
if (optstring[0] != ':' && opterr)
__getopt_msg(argv[0], cnt ?
": option is ambiguous: " :
": unrecognized option: ",
argv[optind]+2,
strlen(argv[optind]+2));
optind++; optind++;
return '?'; return '?';
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册