mirror of
https://git.savannah.gnu.org/git/make.git
synced 2025-02-07 14:56:06 +00:00
(handle_non_switch_argument): New function, broken out of decode_switches.
(decode_switches): Set optind to 0 to reinitialize getopt, not to 1. When getopt_long returns EOF, break the loop and handle remaining args with a simple second loop.
This commit is contained in:
parent
fe05aaf7f2
commit
1331cca1fa
1 changed files with 59 additions and 55 deletions
114
main.c
114
main.c
|
@ -1198,6 +1198,51 @@ init_switches ()
|
||||||
long_options[i].name = 0;
|
long_options[i].name = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
handle_non_switch_argument (arg, env)
|
||||||
|
char *arg;
|
||||||
|
int env;
|
||||||
|
{
|
||||||
|
/* Non-option argument. It might be a variable definition. */
|
||||||
|
struct variable *v;
|
||||||
|
if (arg[0] == '-' && arg[1] == '\0')
|
||||||
|
/* Ignore plain `-' for compatibility. */
|
||||||
|
return;
|
||||||
|
v = try_variable_definition ((char *) 0, 0, arg, o_command);
|
||||||
|
if (v != 0)
|
||||||
|
{
|
||||||
|
/* It is indeed a variable definition. Record a pointer to
|
||||||
|
the variable for later use in define_makeflags. */
|
||||||
|
struct command_variable *cv
|
||||||
|
= (struct command_variable *) xmalloc (sizeof (*cv));
|
||||||
|
cv->variable = v;
|
||||||
|
cv->next = command_variables;
|
||||||
|
command_variables = cv;
|
||||||
|
}
|
||||||
|
else if (! env)
|
||||||
|
{
|
||||||
|
/* Not an option or variable definition; it must be a goal
|
||||||
|
target! Enter it as a file and add it to the dep chain of
|
||||||
|
goals. */
|
||||||
|
struct file *f = enter_command_line_file (arg);
|
||||||
|
f->cmd_target = 1;
|
||||||
|
|
||||||
|
if (goals == 0)
|
||||||
|
{
|
||||||
|
goals = (struct dep *) xmalloc (sizeof (struct dep));
|
||||||
|
lastgoal = goals;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lastgoal->next
|
||||||
|
= (struct dep *) xmalloc (sizeof (struct dep));
|
||||||
|
lastgoal = lastgoal->next;
|
||||||
|
}
|
||||||
|
lastgoal->name = 0;
|
||||||
|
lastgoal->file = f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Decode switches from ARGC and ARGV.
|
/* Decode switches from ARGC and ARGV.
|
||||||
They came from the environment if ENV is nonzero. */
|
They came from the environment if ENV is nonzero. */
|
||||||
|
|
||||||
|
@ -1221,67 +1266,18 @@ decode_switches (argc, argv, env)
|
||||||
but not for options from the environment. */
|
but not for options from the environment. */
|
||||||
opterr = !env;
|
opterr = !env;
|
||||||
/* Reset getopt's state. */
|
/* Reset getopt's state. */
|
||||||
optind = 1;
|
optind = 0;
|
||||||
|
|
||||||
while (optind < argc)
|
while (optind < argc)
|
||||||
{
|
{
|
||||||
/* Parse the next argument. */
|
/* Parse the next argument. */
|
||||||
c = getopt_long (argc, argv, options, long_options, (int *) 0);
|
c = getopt_long (argc, argv, options, long_options, (int *) 0);
|
||||||
if (c == EOF)
|
if (c == EOF)
|
||||||
{
|
/* End of arguments, or "--" marker seen. */
|
||||||
/* There are no more options according to getting getopt, but
|
break;
|
||||||
there are some arguments left. Since we have asked for
|
else if (c == 1)
|
||||||
non-option arguments to be returned in order, I think this
|
/* An argument not starting with a dash. */
|
||||||
only happens when there is a "--" argument to prevent later
|
handle_non_switch_argument (optarg, env);
|
||||||
arguments from being options. Since getopt has finished its
|
|
||||||
job, just update its state variables for the next argument and
|
|
||||||
set C as if it had returned 1, indicating a non-option
|
|
||||||
argument. */
|
|
||||||
optarg = argv[optind++];
|
|
||||||
c = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == 1)
|
|
||||||
{
|
|
||||||
/* Non-option argument. It might be a variable definition. */
|
|
||||||
struct variable *v;
|
|
||||||
if (optarg[0] == '-' && optarg[1] == '\0')
|
|
||||||
/* Ignore plain `-' for compatibility. */
|
|
||||||
continue;
|
|
||||||
v = try_variable_definition ((char *) 0, 0, optarg, o_command);
|
|
||||||
if (v != 0)
|
|
||||||
{
|
|
||||||
/* It is indeed a variable definition. Record a pointer to
|
|
||||||
the variable for later use in define_makeflags. */
|
|
||||||
struct command_variable *cv
|
|
||||||
= (struct command_variable *) xmalloc (sizeof (*cv));
|
|
||||||
cv->variable = v;
|
|
||||||
cv->next = command_variables;
|
|
||||||
command_variables = cv;
|
|
||||||
}
|
|
||||||
else if (! env)
|
|
||||||
{
|
|
||||||
/* Not an option or variable definition; it must be a goal
|
|
||||||
target! Enter it as a file and add it to the dep chain of
|
|
||||||
goals. */
|
|
||||||
struct file *f = enter_command_line_file (optarg);
|
|
||||||
f->cmd_target = 1;
|
|
||||||
|
|
||||||
if (goals == 0)
|
|
||||||
{
|
|
||||||
goals = (struct dep *) xmalloc (sizeof (struct dep));
|
|
||||||
lastgoal = goals;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lastgoal->next
|
|
||||||
= (struct dep *) xmalloc (sizeof (struct dep));
|
|
||||||
lastgoal = lastgoal->next;
|
|
||||||
}
|
|
||||||
lastgoal->name = 0;
|
|
||||||
lastgoal->file = f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (c == '?')
|
else if (c == '?')
|
||||||
/* Bad option. We will print a usage message and die later.
|
/* Bad option. We will print a usage message and die later.
|
||||||
But continue to parse the other options so the user can
|
But continue to parse the other options so the user can
|
||||||
|
@ -1384,6 +1380,14 @@ positive integral argument",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* There are no more options according to getting getopt, but there may
|
||||||
|
be some arguments left. Since we have asked for non-option arguments
|
||||||
|
to be returned in order, this only happens when there is a "--"
|
||||||
|
argument to prevent later arguments from being options. */
|
||||||
|
while (optind < argc)
|
||||||
|
handle_non_switch_argument (argv[optind++], env);
|
||||||
|
|
||||||
|
|
||||||
if (!env && (bad || print_usage_flag))
|
if (!env && (bad || print_usage_flag))
|
||||||
{
|
{
|
||||||
/* Print a nice usage message. */
|
/* Print a nice usage message. */
|
||||||
|
|
Loading…
Reference in a new issue