]>
git.ipfire.org Git - thirdparty/bash.git/blob - braces.c
1 /* braces.c -- code for doing word expansion in curly braces. */
3 /* Copyright (C) 1987-2018 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21 /* Stuff in curly braces gets expanded before all other shell expansions. */
25 #if defined (BRACE_EXPANSION)
27 #if defined (HAVE_UNISTD_H)
29 # include <sys/types.h>
43 typedef char *WORD_DESC
;
44 typedef char **WORD_LIST
;
49 #include "typemax.h" /* INTMAX_MIN, INTMAX_MAX */
52 #include "chartypes.h"
58 #define brace_whitespace(c) (!(c) || (c) == ' ' || (c) == '\t' || (c) == '\n')
60 #define BRACE_SEQ_SPECIFIER ".."
62 extern int asprintf
__P((char **, const char *, ...)) __attribute__((__format__ (printf
, 2, 3)));
65 extern int last_command_exit_value
;
70 Segregate the text into 3 sections: preamble (stuff before an open brace),
71 postamble (stuff after the matching close brace) and amble (stuff after
72 preamble, and before postamble). Expand amble, and then tack on the
73 expansions to preamble. Expand postamble, and tack on the expansions to
77 /* The character which is used to separate arguments. */
78 static const int brace_arg_separator
= ',';
81 static int brace_gobbler
__P((char *, size_t, int *, int));
82 static char **expand_amble
__P((char *, size_t, int));
83 static char **expand_seqterm
__P((char *, size_t));
84 static char **mkseq
__P((intmax_t, intmax_t, intmax_t, int, int));
85 static char **array_concat
__P((char **, char **));
87 static int brace_gobbler ();
88 static char **expand_amble ();
89 static char **expand_seqterm ();
90 static char **mkseq();
91 static char **array_concat ();
101 for (i
= 0; a
[i
]; i
++)
102 printf ("dump_result: a[%d] = -%s-\n", i
, a
[i
]);
106 /* Return an array of strings; the brace expansion of TEXT. */
113 char *preamble
, *postamble
, *amble
;
115 char **tack
, **result
;
120 /* Find the text of the preamble. */
121 tlen
= strlen (text
);
123 #if defined (CSH_BRACE_COMPAT)
124 c
= brace_gobbler (text
, tlen
, &i
, '{'); /* } */
126 /* Make sure that when we exit this loop, c == 0 or text[i] begins a
127 valid brace expansion sequence. */
130 c
= brace_gobbler (text
, tlen
, &i
, '{'); /* } */
132 /* Verify that c begins a valid brace expansion word. If it doesn't, we
133 go on. Loop stops when there are no more open braces in the word. */
136 start
= j
= i
+ 1; /* { */
137 c
= brace_gobbler (text
, tlen
, &j
, '}');
138 if (c
== 0) /* it's not */
154 #endif /* !CSH_BRACE_COMPAT */
156 preamble
= (char *)xmalloc (i
+ 1);
158 strncpy (preamble
, text
, i
);
161 result
= (char **)xmalloc (2 * sizeof (char *));
162 result
[0] = preamble
;
163 result
[1] = (char *)NULL
;
165 /* Special case. If we never found an exciting character, then
166 the preamble is all of the text, so just return that. */
170 /* Find the amble. This is the stuff inside this set of braces. */
172 c
= brace_gobbler (text
, tlen
, &i
, '}');
174 /* What if there isn't a matching close brace? */
178 /* Well, if we found an unquoted BRACE_ARG_SEPARATOR between START
179 and I, then this should be an error. Otherwise, it isn't. */
186 ADVANCE_CHAR (text
, tlen
, j
);
190 if (text
[j
] == brace_arg_separator
)
192 strvec_dispose (result
);
193 last_command_exit_value
= 1;
194 report_error ("no closing `%c' in %s", '}', text
);
195 throw_to_top_level ();
197 ADVANCE_CHAR (text
, tlen
, j
);
200 free (preamble
); /* Same as result[0]; see initialization. */
201 result
[0] = savestring (text
);
206 amble
= substring (text
, start
, i
);
209 amble
= (char *)xmalloc (1 + (i
- start
));
210 strncpy (amble
, &text
[start
], (i
- start
));
218 /* If the amble does not contain an unquoted BRACE_ARG_SEPARATOR, then
219 just return without doing any expansion. */
223 if (amble
[j
] == '\\')
226 ADVANCE_CHAR (amble
, alen
, j
);
230 if (amble
[j
] == brace_arg_separator
)
233 ADVANCE_CHAR (amble
, alen
, j
);
238 tack
= expand_seqterm (amble
, alen
);
241 else if (text
[i
+ 1])
243 /* If the sequence expansion fails (e.g., because the integers
244 overflow), but there is more in the string, try and process
245 the rest of the string, which may contain additional brace
246 expansions. Treat the unexpanded sequence term as a simple
247 string (including the braces). */
248 tack
= strvec_create (2);
249 tack
[0] = savestring (text
+start
-1);
250 tack
[0][i
-start
+2] = '\0';
258 result
[0] = savestring (text
);
264 tack
= expand_amble (amble
, alen
, 0);
266 result
= array_concat (result
, tack
);
269 strvec_dispose (tack
);
271 postamble
= text
+ i
+ 1;
273 if (postamble
&& *postamble
)
275 tack
= brace_expand (postamble
);
276 result
= array_concat (result
, tack
);
278 strvec_dispose (tack
);
284 /* Expand the text found inside of braces. We simply try to split the
285 text at BRACE_ARG_SEPARATORs into separate strings. We then brace
286 expand each slot which needs it, until there are no more slots which
289 expand_amble (text
, tlen
, flags
)
294 char **result
, **partial
, **tresult
;
302 result
= (char **)NULL
;
308 c
= brace_gobbler (text
, tlen
, &i
, brace_arg_separator
);
310 tem
= substring (text
, start
, i
);
312 tem
= (char *)xmalloc (1 + (i
- start
));
313 strncpy (tem
, &text
[start
], (i
- start
));
314 tem
[i
- start
] = '\0';
317 partial
= brace_expand (tem
);
323 register int lr
, lp
, j
;
325 lr
= strvec_len (result
);
326 lp
= strvec_len (partial
);
328 tresult
= strvec_mresize (result
, lp
+ lr
+ 1);
331 internal_error (_("brace expansion: cannot allocate memory for %s"), tem
);
333 strvec_dispose (partial
);
334 strvec_dispose (result
);
335 result
= (char **)NULL
;
341 for (j
= 0; j
< lp
; j
++)
342 result
[lr
+ j
] = partial
[j
];
344 result
[lr
+ j
] = (char *)NULL
;
349 ADVANCE_CHAR (text
, tlen
, i
);
364 # define sh_imaxabs(x) (((x) >= 0) ? (x) : -(x))
367 /* Handle signed arithmetic overflow and underflow. Have to do it this way
368 to avoid compilers optimizing out simpler overflow checks. */
370 /* Make sure that a+b does not exceed MAXV or is smaller than MINV (if b < 0).
371 Assumes that b > 0 if a > 0 and b < 0 if a < 0 */
372 #define ADDOVERFLOW(a,b,minv,maxv) \
373 ((((a) > 0) && ((b) > ((maxv) - (a)))) || \
374 (((a) < 0) && ((b) < ((minv) - (a)))))
376 /* Make sure that a-b is not smaller than MINV or exceeds MAXV (if b < 0).
377 Assumes that b > 0 if a > 0 and b < 0 if a < 0 */
378 #define SUBOVERFLOW(a,b,minv,maxv) \
379 ((((b) > 0) && ((a) < ((minv) + (b)))) || \
380 (((b) < 0) && ((a) > ((maxv) + (b)))))
383 mkseq (start
, end
, incr
, type
, width
)
384 intmax_t start
, end
, incr
;
394 if (start
> end
&& incr
> 0)
396 else if (start
< end
&& incr
< 0)
398 if (incr
== INTMAX_MIN
) /* Don't use -INTMAX_MIN */
399 return ((char **)NULL
);
403 /* Check that end-start will not overflow INTMAX_MIN, INTMAX_MAX. The +3
404 and -2, not strictly necessary, are there because of the way the number
405 of elements and value passed to strvec_create() are calculated below. */
406 if (SUBOVERFLOW (end
, start
, INTMAX_MIN
+3, INTMAX_MAX
-2))
407 return ((char **)NULL
);
409 prevn
= sh_imaxabs (end
- start
);
410 /* Need to check this way in case INT_MAX == INTMAX_MAX */
411 if (INT_MAX
== INTMAX_MAX
&& (ADDOVERFLOW (prevn
, 2, INT_MIN
, INT_MAX
)))
412 return ((char **)NULL
);
413 /* Make sure the assignment to nelem below doesn't end up <= 0 due to
415 else if (ADDOVERFLOW ((prevn
/sh_imaxabs(incr
)), 1, INTMAX_MIN
, INTMAX_MAX
))
416 return ((char **)NULL
);
418 /* XXX - TOFIX: potentially allocating a lot of extra memory if
419 imaxabs(incr) != 1 */
420 /* Instead of a simple nelem = prevn + 1, something like:
421 nelem = (prevn / imaxabs(incr)) + 1;
423 if ((prevn
/ sh_imaxabs (incr
)) > INT_MAX
- 3) /* check int overflow */
424 return ((char **)NULL
);
425 nelem
= (prevn
/ sh_imaxabs(incr
)) + 1;
426 result
= strvec_mcreate (nelem
+ 1);
429 internal_error (_("brace expansion: failed to allocate memory for %u elements"), (unsigned int)nelem
);
430 return ((char **)NULL
);
433 /* Make sure we go through the loop at least once, so {3..3} prints `3' */
441 result
[i
] = (char *)NULL
;
442 strvec_dispose (result
);
443 result
= (char **)NULL
;
448 result
[i
++] = t
= itos (n
);
449 else if (type
== ST_ZINT
)
453 len
= asprintf (&t
, "%0*d", width
, arg
);
458 if (t
= (char *)malloc (2))
466 /* We failed to allocate memory for this number, so we bail. */
469 char *p
, lbuf
[INT_STRLEN_BOUND(intmax_t) + 1];
471 /* Easier to do this than mess around with various intmax_t printf
472 formats (%ld? %lld? %jd?) and PRIdMAX. */
473 p
= inttostr (n
, lbuf
, sizeof (lbuf
));
474 internal_error (_("brace expansion: failed to allocate memory for `%s'"), p
);
475 strvec_dispose (result
);
476 return ((char **)NULL
);
479 /* Handle overflow and underflow of n+incr */
480 if (ADDOVERFLOW (n
, incr
, INTMAX_MIN
, INTMAX_MAX
))
485 if ((incr
< 0 && n
< end
) || (incr
> 0 && n
> end
))
490 result
[i
] = (char *)0;
495 expand_seqterm (text
, tlen
)
500 int lhs_t
, rhs_t
, lhs_l
, rhs_l
, width
;
501 intmax_t lhs_v
, rhs_v
, incr
;
503 char **result
, *ep
, *oep
;
505 t
= strstr (text
, BRACE_SEQ_SPECIFIER
);
507 return ((char **)NULL
);
509 lhs_l
= t
- text
; /* index of start of BRACE_SEQ_SPECIFIER */
510 lhs
= substring (text
, 0, lhs_l
);
511 rhs
= substring (text
, lhs_l
+ sizeof(BRACE_SEQ_SPECIFIER
) - 1, tlen
);
513 if (lhs
[0] == 0 || rhs
[0] == 0)
517 return ((char **)NULL
);
520 /* Now figure out whether LHS and RHS are integers or letters. Both
521 sides have to match. */
522 lhs_t
= (legal_number (lhs
, &tl
)) ? ST_INT
:
523 ((ISALPHA (lhs
[0]) && lhs
[1] == 0) ? ST_CHAR
: ST_BAD
);
525 /* Decide on rhs and whether or not it looks like the user specified
528 if (ISDIGIT (rhs
[0]) || ((rhs
[0] == '+' || rhs
[0] == '-') && ISDIGIT (rhs
[1])))
532 tr
= strtoimax (rhs
, &ep
, 10);
533 if (errno
== ERANGE
|| (ep
&& *ep
!= 0 && *ep
!= '.'))
534 rhs_t
= ST_BAD
; /* invalid */
536 else if (ISALPHA (rhs
[0]) && (rhs
[1] == 0 || rhs
[1] == '.'))
552 if (ep
&& *ep
== '.' && ep
[1] == '.' && ep
[2])
553 incr
= strtoimax (ep
+ 2, &ep
, 10);
554 if (*ep
!= 0 || errno
== ERANGE
)
555 rhs_t
= ST_BAD
; /* invalid incr or overflow */
559 if (lhs_t
!= rhs_t
|| lhs_t
== ST_BAD
|| rhs_t
== ST_BAD
)
563 return ((char **)NULL
);
566 /* OK, we have something. It's either a sequence of integers, ascending
567 or descending, or a sequence or letters, ditto. Generate the sequence,
568 put it into a string vector, and return it. */
570 if (lhs_t
== ST_CHAR
)
572 lhs_v
= (unsigned char)lhs
[0];
573 rhs_v
= (unsigned char)rhs
[0];
578 lhs_v
= tl
; /* integer truncation */
581 /* Decide whether or not the terms need zero-padding */
582 rhs_l
= tlen
- lhs_l
- sizeof (BRACE_SEQ_SPECIFIER
) + 1;
584 if (lhs_l
> 1 && lhs
[0] == '0')
585 width
= lhs_l
, lhs_t
= ST_ZINT
;
586 if (lhs_l
> 2 && lhs
[0] == '-' && lhs
[1] == '0')
587 width
= lhs_l
, lhs_t
= ST_ZINT
;
588 if (rhs_l
> 1 && rhs
[0] == '0' && width
< rhs_l
)
589 width
= rhs_l
, lhs_t
= ST_ZINT
;
590 if (rhs_l
> 2 && rhs
[0] == '-' && rhs
[1] == '0' && width
< rhs_l
)
591 width
= rhs_l
, lhs_t
= ST_ZINT
;
593 if (width
< lhs_l
&& lhs_t
== ST_ZINT
)
595 if (width
< rhs_l
&& lhs_t
== ST_ZINT
)
599 result
= mkseq (lhs_v
, rhs_v
, incr
, lhs_t
, width
);
607 /* Start at INDEX, and skip characters in TEXT. Set INDEX to the
608 index of the character matching SATISFY. This understands about
609 quoting. Return the character that caused us to stop searching;
610 this is either the same as SATISFY, or 0. */
611 /* If SATISFY is `}', we are looking for a brace expression, so we
612 should enforce the rules that govern valid brace expansions:
613 1) to count as an arg separator, a comma or `..' has to be outside
614 an inner set of braces.
617 brace_gobbler (text
, tlen
, indx
, satisfy
)
623 register int i
, c
, quoted
, level
, commas
, pass_next
;
630 level
= quoted
= pass_next
= 0;
631 #if defined (CSH_BRACE_COMPAT)
634 commas
= (satisfy
== '}') ? 0 : 1;
644 ADVANCE_CHAR (text
, tlen
, i
);
651 /* A backslash escapes the next character. This allows backslash to
652 escape the quote character in a double-quoted string. */
653 if (c
== '\\' && (quoted
== 0 || quoted
== '"' || quoted
== '`'))
661 /* If compiling for the shell, treat ${...} like \{...} */
662 if (c
== '$' && text
[i
+1] == '{' && quoted
!= '\'') /* } */
677 /* The shell allows quoted command substitutions */
678 if (quoted
== '"' && c
== '$' && text
[i
+1] == '(') /*)*/
682 ADVANCE_CHAR (text
, tlen
, i
);
689 if (c
== '"' || c
== '\'' || c
== '`')
697 /* Pass new-style command and process substitutions through unchanged. */
698 if ((c
== '$' || c
== '<' || c
== '>') && text
[i
+1] == '(') /* ) */
702 t
= extract_command_subst (text
, &si
, 0);
710 if (c
== satisfy
&& level
== 0 && quoted
== 0 && commas
> 0)
712 /* We ignore an open brace surrounded by whitespace, and also
713 an open brace followed immediately by a close brace preceded
716 ((!i
|| brace_whitespace (text
[i
- 1])) &&
717 (brace_whitespace (text
[i
+ 1]) || text
[i
+ 1] == '}')))
728 else if (c
== '}' && level
)
730 #if !defined (CSH_BRACE_COMPAT)
731 else if (satisfy
== '}' && c
== brace_arg_separator
&& level
== 0)
733 else if (satisfy
== '}' && STREQN (text
+i
, BRACE_SEQ_SPECIFIER
, 2) &&
734 text
[i
+2] != satisfy
&& level
== 0)
739 ADVANCE_CHAR (text
, tlen
, i
);
749 /* Return a new array of strings which is the result of appending each
750 string in ARR2 to each string in ARR1. The resultant array is
751 len (arr1) * len (arr2) long. For convenience, ARR1 (and its contents)
752 are free ()'ed. ARR1 can be NULL, in that case, a new version of ARR2
755 array_concat (arr1
, arr2
)
758 register int i
, j
, len
, len1
, len2
;
759 register char **result
;
762 return (arr2
); /* XXX - see if we can get away without copying? */
765 return (arr1
); /* XXX - caller expects us to free arr1 */
767 /* We can only short-circuit if the array consists of a single null element;
768 otherwise we need to replicate the contents of the other array and
769 prefix (or append, below) an empty element to each one. */
770 if (arr1
[0] && arr1
[0][0] == 0 && arr1
[1] == 0)
772 strvec_dispose (arr1
);
773 return (arr2
); /* XXX - use flags to see if we can avoid copying here */
776 if (arr2
[0] && arr2
[0][0] == 0 && arr2
[1] == 0)
777 return (arr1
); /* XXX - rather than copying and freeing it */
779 len1
= strvec_len (arr1
);
780 len2
= strvec_len (arr2
);
782 result
= (char **)malloc ((1 + (len1
* len2
)) * sizeof (char *));
787 for (i
= 0; i
< len1
; i
++)
789 int strlen_1
= strlen (arr1
[i
]);
791 for (j
= 0; j
< len2
; j
++)
793 result
[len
] = (char *)xmalloc (1 + strlen_1
+ strlen (arr2
[j
]));
794 strcpy (result
[len
], arr1
[i
]);
795 strcpy (result
[len
] + strlen_1
, arr2
[j
]);
802 result
[len
] = (char *)NULL
;
821 return (realloc (p
, n
));
825 internal_error (format
, arg1
, arg2
)
826 char *format
, *arg1
, *arg2
;
828 fprintf (stderr
, format
, arg1
, arg2
);
829 fprintf (stderr
, "\n");
841 fprintf (stderr
, "brace_expand> ");
843 if ((!fgets (example
, 256, stdin
)) ||
844 (strncmp (example
, "quit", 4) == 0))
847 if (strlen (example
))
848 example
[strlen (example
) - 1] = '\0';
850 result
= brace_expand (example
);
852 for (i
= 0; result
[i
]; i
++)
853 printf ("%s\n", result
[i
]);
855 strvec_dispose (result
);
861 * compile-command: "gcc -g -Bstatic -DTEST -o brace_expand braces.c general.o"
866 #endif /* BRACE_EXPANSION */