commit bash-20080522 snapshot

This commit is contained in:
Chet Ramey
2011-12-07 09:24:08 -05:00
parent 33fe8777ce
commit 8943768b87
74 changed files with 3052 additions and 462 deletions
+62 -14
View File
@@ -1,6 +1,6 @@
/* braces.c -- code for doing word expansion in curly braces. */
/* Copyright (C) 1987-2003 Free Software Foundation, Inc.
/* Copyright (C) 1987-2008 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
@@ -61,7 +61,7 @@ static const int brace_arg_separator = ',';
static int brace_gobbler __P((char *, size_t, int *, int));
static char **expand_amble __P((char *, size_t, int));
static char **expand_seqterm __P((char *, size_t));
static char **mkseq __P((int, int, int, int));
static char **mkseq __P((int, int, int, int, int));
static char **array_concat __P((char **, char **));
#else
static int brace_gobbler ();
@@ -301,10 +301,11 @@ expand_amble (text, tlen, flags)
#define ST_BAD 0
#define ST_INT 1
#define ST_CHAR 2
#define ST_ZINT 3
static char **
mkseq (start, end, incr, type)
int start, end, incr, type;
mkseq (start, end, incr, type, width)
int start, end, incr, type, width;
{
int n, i;
char **result, *t;
@@ -330,6 +331,12 @@ mkseq (start, end, incr, type)
#endif
if (type == ST_INT)
result[i++] = itos (n);
else if (type == ST_ZINT)
{
int len;
len = asprintf (&t, "%0*d", width, n);
result[i++] = t;
}
else
{
t = (char *)xmalloc (2);
@@ -337,9 +344,9 @@ mkseq (start, end, incr, type)
t[1] = '\0';
result[i++] = t;
}
if (n == end)
break;
n += incr;
if ((incr < 0 && n < end) || (incr > 0 && n > end))
break;
}
while (1);
@@ -353,17 +360,17 @@ expand_seqterm (text, tlen)
size_t tlen;
{
char *t, *lhs, *rhs;
int i, lhs_t, rhs_t, lhs_v, rhs_v;
int i, lhs_t, rhs_t, lhs_v, rhs_v, incr, lhs_l, rhs_l, width;
intmax_t tl, tr;
char **result;
char **result, *ep;
t = strstr (text, BRACE_SEQ_SPECIFIER);
if (t == 0)
return ((char **)NULL);
i = t - text; /* index of start of BRACE_SEQ_SPECIFIER */
lhs = substring (text, 0, i);
rhs = substring (text, i + sizeof(BRACE_SEQ_SPECIFIER) - 1, tlen);
lhs_l = t - text; /* index of start of BRACE_SEQ_SPECIFIER */
lhs = substring (text, 0, lhs_l);
rhs = substring (text, lhs_l + sizeof(BRACE_SEQ_SPECIFIER) - 1, tlen);
if (lhs[0] == 0 || rhs[0] == 0)
{
@@ -376,8 +383,36 @@ expand_seqterm (text, tlen)
sides have to match. */
lhs_t = (legal_number (lhs, &tl)) ? ST_INT :
((ISALPHA (lhs[0]) && lhs[1] == 0) ? ST_CHAR : ST_BAD);
rhs_t = (legal_number (rhs, &tr)) ? ST_INT :
((ISALPHA (rhs[0]) && rhs[1] == 0) ? ST_CHAR : ST_BAD);
/* Decide on rhs and whether or not it looks like the user specified
an increment */
ep = 0;
if (ISDIGIT (rhs[0]) || ((rhs[0] == '+' || rhs[0] == '-') && ISDIGIT (rhs[1])))
{
rhs_t = ST_INT;
tr = strtoimax (rhs, &ep, 10);
if (ep && *ep != 0 && *ep != '.')
rhs_t = ST_BAD; /* invalid */
}
else if (ISALPHA (rhs[0]) && (rhs[1] == 0 || rhs[1] == '.'))
{
rhs_t = ST_CHAR;
ep = rhs + 1;
}
else
{
rhs_t = ST_BAD;
ep = 0;
}
incr = 1;
if (rhs_t != ST_BAD)
{
if (ep && *ep == '.' && ep[1] == '.' && ep[2])
incr = strtoimax (ep + 2, &ep, 10);
if (*ep != 0)
rhs_t = ST_BAD; /* invalid incr */
}
if (lhs_t != rhs_t || lhs_t == ST_BAD || rhs_t == ST_BAD)
{
@@ -394,14 +429,27 @@ expand_seqterm (text, tlen)
{
lhs_v = (unsigned char)lhs[0];
rhs_v = (unsigned char)rhs[0];
width = 1;
}
else
{
lhs_v = tl; /* integer truncation */
rhs_v = tr;
/* Decide whether or not the terms need zero-padding */
rhs_l = tlen - lhs_l - sizeof (BRACE_SEQ_SPECIFIER) + 1;
width = 0;
if (lhs_l > 1 && lhs[0] == '0')
width = lhs_l, lhs_t = ST_ZINT;
if (lhs_l > 2 && lhs[0] == '-' && lhs[1] == '0')
width = lhs_l, lhs_t = ST_ZINT;
if (rhs_l > 1 && rhs[0] == '0' && width < rhs_l)
width = rhs_l, lhs_t = ST_ZINT;
if (rhs_l > 2 && rhs[0] == '-' && rhs[1] == '0' && width < rhs_l)
width = rhs_l, lhs_t = ST_ZINT;
}
result = mkseq (lhs_v, rhs_v, 1, lhs_t);
result = mkseq (lhs_v, rhs_v, incr, lhs_t, width);
free (lhs);
free (rhs);