44 #ifdef PLATFORM_WINDOWS
62 static void warnx(
int print_error,
const char *fmt, ...)
67 _vsnprintf(opterrmsg, 128, fmt, ap);
72 fprintf(stderr, opterrmsg);
73 fprintf(stderr,
"\n");
81 #define _DIAGASSERT(X)
84 #if HAVE_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
85 #define REPLACE_GETOPT
90 __weak_alias(
getopt,_getopt)
97 #elif HAVE_CONFIG_H && !HAVE_DECL_OPTRESET
105 #if !HAVE_GETOPT_LONG
106 #define IGNORE_FIRST (*options == '-' || *options == '+')
107 #define PRINT_ERROR ((opterr) && ((*options != ':') \
108 || (IGNORE_FIRST && options[1] != ':')))
109 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
110 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
112 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
115 #define BADCH (int)'?'
116 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
117 || (*options == ':') ? (int)':' : (int)'?')
118 #define INORDER (int)1
123 static int gcd(
int,
int);
124 static void permute_args(
int,
int,
int,
char *
const *);
133 static const char recargchar[] =
"option requires an argument -- %c";
134 static const char recargstring[] =
"option requires an argument -- %s";
135 static const char ambig[] =
"ambiguous option -- %.*s";
136 static const char noarg[] =
"option doesn't take an argument -- %.*s";
170 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
178 nnonopts = panonopt_end - panonopt_start;
179 nopts = opt_end - panonopt_end;
180 ncycle =
gcd(nnonopts, nopts);
181 cyclelen = (opt_end - panonopt_start) / ncycle;
183 for (i = 0; i < ncycle; i++) {
184 cstart = panonopt_end+i;
186 for (j = 0; j < cyclelen; j++) {
187 if (pos >= panonopt_end)
193 ((
char **) nargv)[pos] = nargv[cstart];
195 ((
char **)nargv)[cstart] =
swap;
227 nonopt_start = nonopt_end = -1;
229 if (optreset || !*place) {
231 if (optind >= nargc) {
233 if (nonopt_end != -1) {
239 else if (nonopt_start != -1) {
246 nonopt_start = nonopt_end = -1;
249 if ((*(place = nargv[optind]) !=
'-')
250 || (place[1] ==
'\0')) {
257 optarg = nargv[optind++];
268 if (nonopt_start == -1)
270 else if (nonopt_end != -1) {
273 nonopt_start = optind -
281 if (nonopt_start != -1 && nonopt_end == -1)
283 if (place[1] && *++place ==
'-') {
288 if ((optchar = (
int)*place++) == (int)
':' ||
289 (oli = (
char*)strchr(options + (
IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
293 #ifndef PLATFORM_WINDOWS
295 warnx(illoptchar, optchar);
302 if (optchar ==
'W' && oli[1] ==
';') {
307 if (++optind >= nargc) {
309 #ifndef PLATFORM_WINDOWS
311 warnx(recargchar, optchar);
333 else if (oli[1] !=
':') {
334 if (++optind >= nargc) {
336 #ifndef PLATFORM_WINDOWS
338 warnx(recargchar, optchar);
355 #ifdef REPLACE_GETOPT
378 if (nonopt_end != -1) {
383 nonopt_start = nonopt_end = -1;
398 const struct option *long_options,
409 char *current_argv, *has_equal;
410 size_t current_argv_len;
413 current_argv =
place;
419 if (*current_argv ==
'\0') {
424 if (nonopt_end != -1) {
429 nonopt_start = nonopt_end = -1;
432 if ((has_equal = strchr(current_argv,
'=')) != NULL) {
434 current_argv_len = has_equal - current_argv;
437 current_argv_len = strlen(current_argv);
439 for (i = 0; long_options[i].
name; i++) {
441 if (strncmp(current_argv, long_options[i].
name,
445 if (strlen(long_options[i].name) ==
446 (
unsigned)current_argv_len) {
455 #ifndef PLATFORM_WINDOWS
457 warnx(ambig, (
int)current_argv_len,
470 #ifndef PLATFORM_WINDOWS
472 warnx(noarg, (
int)current_argv_len,
482 if (long_options[match].flag == NULL)
483 optopt = long_options[match].
val;
492 else if (long_options[match].has_arg ==
498 optarg = nargv[optind++];
502 && (optarg == NULL)) {
507 #ifndef PLATFORM_WINDOWS
509 warnx(recargstring, current_argv);
517 if (long_options[match].flag == NULL)
518 optopt = long_options[match].
val;
525 #ifndef PLATFORM_WINDOWS
527 warnx(illoptstring, current_argv);
534 if (long_options[match].flag) {
535 *long_options[match].
flag = long_options[match].
val;
538 retval = long_options[match].
val;
static const char illoptchar[]
static void permute_args(int, int, int, char *const *)
int getopt(int nargc, char *const *nargv, const char *ostr)
static const char recargstring[]
static int getopt_internal(int, char *const *, const char *)
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > start
static const char recargchar[]
__BEGIN_DECLS GETOPT_API int getopt_long(int nargc, char *const *nargv, const char *options, const struct option *long_options, int *idx)
void swap(shared_ptr< T > &a, shared_ptr< T > &b)
__BEGIN_DECLS GETOPT_API int opterr
static const char illoptstring[]
#define optional_argument
static const char noarg[]
#define required_argument
static const char ambig[]