i-bash/builtins/bashgetopt.c

182 lines
4.6 KiB
C
Raw Normal View History

1996-08-26 18:22:31 +00:00
/* bashgetopt.c -- `getopt' for use by the builtins. */
2002-07-17 14:10:11 +00:00
/* Copyright (C) 1992-2002 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
#include <config.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "../bashansi.h"
2001-11-13 17:56:06 +00:00
#include <chartypes.h>
1996-08-26 18:22:31 +00:00
#include <errno.h>
1996-12-23 17:02:34 +00:00
#include "../shell.h"
#include "common.h"
1996-08-26 18:22:31 +00:00
#include "bashgetopt.h"
2002-07-17 14:10:11 +00:00
#define ISOPT(s) (((*(s) == '-') || (plus && *(s) == '+')) && (s)[1])
#define NOTOPT(s) (((*(s) != '-') && (!plus || *(s) != '+')) || (s)[1] == '\0')
1996-08-26 18:22:31 +00:00
static int sp;
char *list_optarg;
int list_optopt;
2002-07-17 14:10:11 +00:00
int list_opttype;
1996-08-26 18:22:31 +00:00
static WORD_LIST *lhead = (WORD_LIST *)NULL;
WORD_LIST *lcurrent = (WORD_LIST *)NULL;
WORD_LIST *loptend; /* Points to the first non-option argument in the list */
int
internal_getopt(list, opts)
WORD_LIST *list;
char *opts;
{
register int c;
register char *cp;
1996-12-23 17:02:34 +00:00
int plus; /* nonzero means to handle +option */
2002-07-17 14:10:11 +00:00
static char errstr[3] = { '-', '\0', '\0' };
1996-08-26 18:22:31 +00:00
2002-07-17 14:10:11 +00:00
plus = *opts == '+';
if (plus)
1996-12-23 17:02:34 +00:00
opts++;
if (list == 0) {
1996-08-26 18:22:31 +00:00
list_optarg = (char *)NULL;
loptend = (WORD_LIST *)NULL; /* No non-option arguments */
return -1;
}
1996-12-23 17:02:34 +00:00
if (list != lhead || lhead == 0) {
1996-08-26 18:22:31 +00:00
/* Hmmm.... called with a different word list. Reset. */
sp = 1;
lcurrent = lhead = list;
loptend = (WORD_LIST *)NULL;
}
if (sp == 1) {
2002-07-17 14:10:11 +00:00
if (lcurrent == 0 || NOTOPT(lcurrent->word->word)) {
1996-08-26 18:22:31 +00:00
lhead = (WORD_LIST *)NULL;
loptend = lcurrent;
return(-1);
} else if (ISHELP (lcurrent->word->word)) {
lhead = (WORD_LIST *)NULL;
loptend = lcurrent;
return (GETOPT_HELP);
1996-08-26 18:22:31 +00:00
} else if (lcurrent->word->word[0] == '-' &&
lcurrent->word->word[1] == '-' &&
lcurrent->word->word[2] == 0) {
lhead = (WORD_LIST *)NULL;
loptend = lcurrent->next;
return(-1);
}
2002-07-17 14:10:11 +00:00
errstr[0] = list_opttype = lcurrent->word->word[0];
1996-08-26 18:22:31 +00:00
}
list_optopt = c = lcurrent->word->word[sp];
if (c == ':' || (cp = strchr(opts, c)) == NULL) {
2002-07-17 14:10:11 +00:00
errstr[1] = c;
sh_invalidopt (errstr);
1996-08-26 18:22:31 +00:00
if (lcurrent->word->word[++sp] == '\0') {
lcurrent = lcurrent->next;
sp = 1;
}
list_optarg = NULL;
if (lcurrent)
loptend = lcurrent->next;
return('?');
}
1996-12-23 17:02:34 +00:00
if (*++cp == ':' || *cp == ';') {
/* `:': Option requires an argument. */
/* `;': option argument may be missing */
1996-08-26 18:22:31 +00:00
/* We allow -l2 as equivalent to -l 2 */
1996-12-23 17:02:34 +00:00
if (lcurrent->word->word[sp+1]) {
list_optarg = lcurrent->word->word + sp + 1;
lcurrent = lcurrent->next;
/* If the specifier is `;', don't set optarg if the next
argument looks like another option. */
2002-07-17 14:10:11 +00:00
#if 0
1996-12-23 17:02:34 +00:00
} else if (lcurrent->next && (*cp == ':' || lcurrent->next->word->word[0] != '-')) {
2002-07-17 14:10:11 +00:00
#else
} else if (lcurrent->next && (*cp == ':' || NOTOPT(lcurrent->next->word->word))) {
#endif
1996-12-23 17:02:34 +00:00
lcurrent = lcurrent->next;
list_optarg = lcurrent->word->word;
lcurrent = lcurrent->next;
} else if (*cp == ';') {
list_optarg = (char *)NULL;
1996-08-26 18:22:31 +00:00
lcurrent = lcurrent->next;
1996-12-23 17:02:34 +00:00
} else { /* lcurrent->next == NULL */
2002-07-17 14:10:11 +00:00
errstr[1] = c;
sh_needarg (errstr);
1996-08-26 18:22:31 +00:00
sp = 1;
list_optarg = (char *)NULL;
return('?');
}
sp = 1;
1996-12-23 17:02:34 +00:00
} else if (*cp == '#') {
2002-07-17 14:10:11 +00:00
/* option requires a numeric argument */
1996-12-23 17:02:34 +00:00
if (lcurrent->word->word[sp+1]) {
2001-11-13 17:56:06 +00:00
if (DIGIT(lcurrent->word->word[sp+1])) {
1996-12-23 17:02:34 +00:00
list_optarg = lcurrent->word->word + sp + 1;
lcurrent = lcurrent->next;
} else
list_optarg = (char *)NULL;
} else {
2002-07-17 14:10:11 +00:00
if (lcurrent->next && legal_number(lcurrent->next->word->word, (intmax_t *)0)) {
1996-12-23 17:02:34 +00:00
lcurrent = lcurrent->next;
list_optarg = lcurrent->word->word;
lcurrent = lcurrent->next;
2002-07-17 14:10:11 +00:00
} else {
errstr[1] = c;
sh_neednumarg (errstr);
sp = 1;
1996-12-23 17:02:34 +00:00
list_optarg = (char *)NULL;
2002-07-17 14:10:11 +00:00
return ('?');
}
1996-12-23 17:02:34 +00:00
}
1996-08-26 18:22:31 +00:00
} else {
/* No argument, just return the option. */
if (lcurrent->word->word[++sp] == '\0') {
sp = 1;
lcurrent = lcurrent->next;
}
list_optarg = (char *)NULL;
}
return(c);
}
/*
* reset_internal_getopt -- force the in[ft]ernal getopt to reset
*/
void
reset_internal_getopt ()
{
lhead = lcurrent = loptend = (WORD_LIST *)NULL;
sp = 1;
}