Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* braces.c -- code for doing word expansion in curly braces. */ |
| 2 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3 | /* Copyright (C) 1987-2009 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 16 | |
| 17 | You should have received a copy of the GNU General Public License |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 21 | /* Stuff in curly braces gets expanded before all other shell expansions. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 22 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
| 25 | #if defined (BRACE_EXPANSION) |
| 26 | |
| 27 | #if defined (HAVE_UNISTD_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 28 | # ifdef _MINIX |
| 29 | # include <sys/types.h> |
| 30 | # endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 31 | # include <unistd.h> |
| 32 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 33 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 34 | #include "bashansi.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 35 | |
| 36 | #if defined (SHELL) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 37 | # include "shell.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 38 | #endif /* SHELL */ |
| 39 | |
| 40 | #include "general.h" |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 41 | #include "shmbutil.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 42 | #include "chartypes.h" |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 43 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 44 | #define brace_whitespace(c) (!(c) || (c) == ' ' || (c) == '\t' || (c) == '\n') |
| 45 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 46 | #define BRACE_SEQ_SPECIFIER ".." |
| 47 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 48 | extern int asprintf __P((char **, const char *, ...)) __attribute__((__format__ (printf, 2, 3))); |
| 49 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 50 | /* Basic idea: |
| 51 | |
| 52 | Segregate the text into 3 sections: preamble (stuff before an open brace), |
| 53 | postamble (stuff after the matching close brace) and amble (stuff after |
| 54 | preamble, and before postamble). Expand amble, and then tack on the |
| 55 | expansions to preamble. Expand postamble, and tack on the expansions to |
| 56 | the result so far. |
| 57 | */ |
| 58 | |
| 59 | /* The character which is used to separate arguments. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 60 | static const int brace_arg_separator = ','; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 61 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 62 | #if defined (__P) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 63 | static int brace_gobbler __P((char *, size_t, int *, int)); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 64 | static char **expand_amble __P((char *, size_t, int)); |
| 65 | static char **expand_seqterm __P((char *, size_t)); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame^] | 66 | static char **mkseq __P((intmax_t, intmax_t, int, int, int)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 67 | static char **array_concat __P((char **, char **)); |
| 68 | #else |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 69 | static int brace_gobbler (); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 70 | static char **expand_amble (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 71 | static char **expand_seqterm (); |
| 72 | static char **mkseq(); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 73 | static char **array_concat (); |
| 74 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 75 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 76 | #if 0 |
| 77 | static void |
| 78 | dump_result (a) |
| 79 | char **a; |
| 80 | { |
| 81 | int i; |
| 82 | |
| 83 | for (i = 0; a[i]; i++) |
| 84 | printf ("dump_result: a[%d] = -%s-\n", i, a[i]); |
| 85 | } |
| 86 | #endif |
| 87 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 88 | /* Return an array of strings; the brace expansion of TEXT. */ |
| 89 | char ** |
| 90 | brace_expand (text) |
| 91 | char *text; |
| 92 | { |
| 93 | register int start; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 94 | size_t tlen; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 95 | char *preamble, *postamble, *amble; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 96 | size_t alen; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 97 | char **tack, **result; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 98 | int i, j, c, c1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 99 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 100 | DECLARE_MBSTATE; |
| 101 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 102 | /* Find the text of the preamble. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 103 | tlen = strlen (text); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 104 | i = 0; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 105 | #if defined (CSH_BRACE_COMPAT) |
| 106 | c = brace_gobbler (text, tlen, &i, '{'); /* } */ |
| 107 | #else |
| 108 | /* Make sure that when we exit this loop, c == 0 or text[i] begins a |
| 109 | valid brace expansion sequence. */ |
| 110 | do |
| 111 | { |
| 112 | c = brace_gobbler (text, tlen, &i, '{'); /* } */ |
| 113 | c1 = c; |
| 114 | /* Verify that c begins a valid brace expansion word. If it doesn't, we |
| 115 | go on. Loop stops when there are no more open braces in the word. */ |
| 116 | if (c) |
| 117 | { |
| 118 | start = j = i + 1; /* { */ |
| 119 | c = brace_gobbler (text, tlen, &j, '}'); |
| 120 | if (c == 0) /* it's not */ |
| 121 | { |
| 122 | i++; |
| 123 | c = c1; |
| 124 | continue; |
| 125 | } |
| 126 | else /* it is */ |
| 127 | { |
| 128 | c = c1; |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | else |
| 133 | break; |
| 134 | } |
| 135 | while (c); |
| 136 | #endif /* !CSH_BRACE_COMPAT */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 137 | |
| 138 | preamble = (char *)xmalloc (i + 1); |
| 139 | strncpy (preamble, text, i); |
| 140 | preamble[i] = '\0'; |
| 141 | |
| 142 | result = (char **)xmalloc (2 * sizeof (char *)); |
| 143 | result[0] = preamble; |
| 144 | result[1] = (char *)NULL; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 145 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 146 | /* Special case. If we never found an exciting character, then |
| 147 | the preamble is all of the text, so just return that. */ |
| 148 | if (c != '{') |
| 149 | return (result); |
| 150 | |
| 151 | /* Find the amble. This is the stuff inside this set of braces. */ |
| 152 | start = ++i; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 153 | c = brace_gobbler (text, tlen, &i, '}'); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 154 | |
| 155 | /* What if there isn't a matching close brace? */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 156 | if (c == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 157 | { |
| 158 | #if defined (NOTDEF) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 159 | /* Well, if we found an unquoted BRACE_ARG_SEPARATOR between START |
| 160 | and I, then this should be an error. Otherwise, it isn't. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 161 | j = start; |
| 162 | while (j < i) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 163 | { |
| 164 | if (text[j] == '\\') |
| 165 | { |
| 166 | j++; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 167 | ADVANCE_CHAR (text, tlen, j); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 168 | continue; |
| 169 | } |
| 170 | |
| 171 | if (text[j] == brace_arg_separator) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 172 | { /* { */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 173 | strvec_dispose (result); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 174 | report_error ("no closing `%c' in %s", '}', text); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 175 | throw_to_top_level (); |
| 176 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 177 | ADVANCE_CHAR (text, tlen, j); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 178 | } |
| 179 | #endif |
| 180 | free (preamble); /* Same as result[0]; see initialization. */ |
| 181 | result[0] = savestring (text); |
| 182 | return (result); |
| 183 | } |
| 184 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 185 | #if defined (SHELL) |
| 186 | amble = substring (text, start, i); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 187 | alen = i - start; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 188 | #else |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 189 | amble = (char *)xmalloc (1 + (i - start)); |
| 190 | strncpy (amble, &text[start], (i - start)); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 191 | alen = i - start; |
| 192 | amble[alen] = '\0'; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 193 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 194 | |
| 195 | #if defined (SHELL) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 196 | INITIALIZE_MBSTATE; |
| 197 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 198 | /* If the amble does not contain an unquoted BRACE_ARG_SEPARATOR, then |
| 199 | just return without doing any expansion. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 200 | j = 0; |
| 201 | while (amble[j]) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 202 | { |
| 203 | if (amble[j] == '\\') |
| 204 | { |
| 205 | j++; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 206 | ADVANCE_CHAR (amble, alen, j); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 207 | continue; |
| 208 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 209 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 210 | if (amble[j] == brace_arg_separator) |
| 211 | break; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 212 | |
| 213 | ADVANCE_CHAR (amble, alen, j); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 214 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 215 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 216 | if (amble[j] == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 217 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 218 | tack = expand_seqterm (amble, alen); |
| 219 | if (tack) |
| 220 | goto add_tack; |
| 221 | else |
| 222 | { |
| 223 | free (amble); |
| 224 | free (preamble); |
| 225 | result[0] = savestring (text); |
| 226 | return (result); |
| 227 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 228 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 229 | #endif /* SHELL */ |
| 230 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 231 | tack = expand_amble (amble, alen, 0); |
| 232 | add_tack: |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 233 | result = array_concat (result, tack); |
| 234 | free (amble); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 235 | strvec_dispose (tack); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 236 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 237 | postamble = text + i + 1; |
| 238 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 239 | tack = brace_expand (postamble); |
| 240 | result = array_concat (result, tack); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 241 | strvec_dispose (tack); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 242 | |
| 243 | return (result); |
| 244 | } |
| 245 | |
| 246 | /* Expand the text found inside of braces. We simply try to split the |
| 247 | text at BRACE_ARG_SEPARATORs into separate strings. We then brace |
| 248 | expand each slot which needs it, until there are no more slots which |
| 249 | need it. */ |
| 250 | static char ** |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 251 | expand_amble (text, tlen, flags) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 252 | char *text; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 253 | size_t tlen; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 254 | int flags; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 255 | { |
| 256 | char **result, **partial; |
| 257 | char *tem; |
| 258 | int start, i, c; |
| 259 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 260 | DECLARE_MBSTATE; |
| 261 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 262 | result = (char **)NULL; |
| 263 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 264 | start = i = 0; |
| 265 | c = 1; |
| 266 | while (c) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 267 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 268 | c = brace_gobbler (text, tlen, &i, brace_arg_separator); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 269 | #if defined (SHELL) |
| 270 | tem = substring (text, start, i); |
| 271 | #else |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 272 | tem = (char *)xmalloc (1 + (i - start)); |
| 273 | strncpy (tem, &text[start], (i - start)); |
| 274 | tem[i- start] = '\0'; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 275 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 276 | |
| 277 | partial = brace_expand (tem); |
| 278 | |
| 279 | if (!result) |
| 280 | result = partial; |
| 281 | else |
| 282 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 283 | register int lr, lp, j; |
| 284 | |
| 285 | lr = strvec_len (result); |
| 286 | lp = strvec_len (partial); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 287 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 288 | result = strvec_resize (result, lp + lr + 1); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 289 | |
| 290 | for (j = 0; j < lp; j++) |
| 291 | result[lr + j] = partial[j]; |
| 292 | |
| 293 | result[lr + j] = (char *)NULL; |
| 294 | free (partial); |
| 295 | } |
| 296 | free (tem); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 297 | ADVANCE_CHAR (text, tlen, i); |
| 298 | start = i; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 299 | } |
| 300 | return (result); |
| 301 | } |
| 302 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 303 | #define ST_BAD 0 |
| 304 | #define ST_INT 1 |
| 305 | #define ST_CHAR 2 |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 306 | #define ST_ZINT 3 |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 307 | |
| 308 | static char ** |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 309 | mkseq (start, end, incr, type, width) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame^] | 310 | intmax_t start, end; |
| 311 | int incr, type, width; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 312 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame^] | 313 | intmax_t n; |
| 314 | int i; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 315 | char **result, *t; |
| 316 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame^] | 317 | i = abs (end - start) + 1; |
| 318 | result = strvec_create (i + 1); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 319 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 320 | if (incr == 0) |
| 321 | incr = 1; |
| 322 | |
| 323 | if (start > end && incr > 0) |
| 324 | incr = -incr; |
| 325 | else if (start < end && incr < 0) |
| 326 | incr = -incr; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 327 | |
| 328 | /* Make sure we go through the loop at least once, so {3..3} prints `3' */ |
| 329 | i = 0; |
| 330 | n = start; |
| 331 | do |
| 332 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 333 | #if defined (SHELL) |
| 334 | QUIT; /* XXX - memory leak here */ |
| 335 | #endif |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 336 | if (type == ST_INT) |
| 337 | result[i++] = itos (n); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 338 | else if (type == ST_ZINT) |
| 339 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame^] | 340 | int len, arg; |
| 341 | arg = n; |
| 342 | len = asprintf (&t, "%0*d", width, arg); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 343 | result[i++] = t; |
| 344 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 345 | else |
| 346 | { |
| 347 | t = (char *)xmalloc (2); |
| 348 | t[0] = n; |
| 349 | t[1] = '\0'; |
| 350 | result[i++] = t; |
| 351 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 352 | n += incr; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 353 | if ((incr < 0 && n < end) || (incr > 0 && n > end)) |
| 354 | break; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 355 | } |
| 356 | while (1); |
| 357 | |
| 358 | result[i] = (char *)0; |
| 359 | return (result); |
| 360 | } |
| 361 | |
| 362 | static char ** |
| 363 | expand_seqterm (text, tlen) |
| 364 | char *text; |
| 365 | size_t tlen; |
| 366 | { |
| 367 | char *t, *lhs, *rhs; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame^] | 368 | int i, lhs_t, rhs_t, incr, lhs_l, rhs_l, width; |
| 369 | intmax_t lhs_v, rhs_v; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 370 | intmax_t tl, tr; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 371 | char **result, *ep, *oep; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 372 | |
| 373 | t = strstr (text, BRACE_SEQ_SPECIFIER); |
| 374 | if (t == 0) |
| 375 | return ((char **)NULL); |
| 376 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 377 | lhs_l = t - text; /* index of start of BRACE_SEQ_SPECIFIER */ |
| 378 | lhs = substring (text, 0, lhs_l); |
| 379 | rhs = substring (text, lhs_l + sizeof(BRACE_SEQ_SPECIFIER) - 1, tlen); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 380 | |
| 381 | if (lhs[0] == 0 || rhs[0] == 0) |
| 382 | { |
| 383 | free (lhs); |
| 384 | free (rhs); |
| 385 | return ((char **)NULL); |
| 386 | } |
| 387 | |
| 388 | /* Now figure out whether LHS and RHS are integers or letters. Both |
| 389 | sides have to match. */ |
| 390 | lhs_t = (legal_number (lhs, &tl)) ? ST_INT : |
| 391 | ((ISALPHA (lhs[0]) && lhs[1] == 0) ? ST_CHAR : ST_BAD); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 392 | |
| 393 | /* Decide on rhs and whether or not it looks like the user specified |
| 394 | an increment */ |
| 395 | ep = 0; |
| 396 | if (ISDIGIT (rhs[0]) || ((rhs[0] == '+' || rhs[0] == '-') && ISDIGIT (rhs[1]))) |
| 397 | { |
| 398 | rhs_t = ST_INT; |
| 399 | tr = strtoimax (rhs, &ep, 10); |
| 400 | if (ep && *ep != 0 && *ep != '.') |
| 401 | rhs_t = ST_BAD; /* invalid */ |
| 402 | } |
| 403 | else if (ISALPHA (rhs[0]) && (rhs[1] == 0 || rhs[1] == '.')) |
| 404 | { |
| 405 | rhs_t = ST_CHAR; |
| 406 | ep = rhs + 1; |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | rhs_t = ST_BAD; |
| 411 | ep = 0; |
| 412 | } |
| 413 | |
| 414 | incr = 1; |
| 415 | if (rhs_t != ST_BAD) |
| 416 | { |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 417 | oep = ep; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 418 | if (ep && *ep == '.' && ep[1] == '.' && ep[2]) |
| 419 | incr = strtoimax (ep + 2, &ep, 10); |
| 420 | if (*ep != 0) |
| 421 | rhs_t = ST_BAD; /* invalid incr */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 422 | tlen -= ep - oep; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 423 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 424 | |
| 425 | if (lhs_t != rhs_t || lhs_t == ST_BAD || rhs_t == ST_BAD) |
| 426 | { |
| 427 | free (lhs); |
| 428 | free (rhs); |
| 429 | return ((char **)NULL); |
| 430 | } |
| 431 | |
| 432 | /* OK, we have something. It's either a sequence of integers, ascending |
| 433 | or descending, or a sequence or letters, ditto. Generate the sequence, |
| 434 | put it into a string vector, and return it. */ |
| 435 | |
| 436 | if (lhs_t == ST_CHAR) |
| 437 | { |
Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 438 | lhs_v = (unsigned char)lhs[0]; |
| 439 | rhs_v = (unsigned char)rhs[0]; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 440 | width = 1; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 441 | } |
| 442 | else |
| 443 | { |
| 444 | lhs_v = tl; /* integer truncation */ |
| 445 | rhs_v = tr; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 446 | |
| 447 | /* Decide whether or not the terms need zero-padding */ |
| 448 | rhs_l = tlen - lhs_l - sizeof (BRACE_SEQ_SPECIFIER) + 1; |
| 449 | width = 0; |
| 450 | if (lhs_l > 1 && lhs[0] == '0') |
| 451 | width = lhs_l, lhs_t = ST_ZINT; |
| 452 | if (lhs_l > 2 && lhs[0] == '-' && lhs[1] == '0') |
| 453 | width = lhs_l, lhs_t = ST_ZINT; |
| 454 | if (rhs_l > 1 && rhs[0] == '0' && width < rhs_l) |
| 455 | width = rhs_l, lhs_t = ST_ZINT; |
| 456 | if (rhs_l > 2 && rhs[0] == '-' && rhs[1] == '0' && width < rhs_l) |
| 457 | width = rhs_l, lhs_t = ST_ZINT; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 458 | |
| 459 | if (width < lhs_l && lhs_t == ST_ZINT) |
| 460 | width = lhs_l; |
| 461 | if (width < rhs_l && lhs_t == ST_ZINT) |
| 462 | width = rhs_l; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 465 | result = mkseq (lhs_v, rhs_v, incr, lhs_t, width); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 466 | |
| 467 | free (lhs); |
| 468 | free (rhs); |
| 469 | |
| 470 | return (result); |
| 471 | } |
| 472 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 473 | /* Start at INDEX, and skip characters in TEXT. Set INDEX to the |
| 474 | index of the character matching SATISFY. This understands about |
| 475 | quoting. Return the character that caused us to stop searching; |
| 476 | this is either the same as SATISFY, or 0. */ |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 477 | /* If SATISFY is `}', we are looking for a brace expression, so we |
| 478 | should enforce the rules that govern valid brace expansions: |
| 479 | 1) to count as an arg separator, a comma or `..' has to be outside |
| 480 | an inner set of braces. |
| 481 | */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 482 | static int |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 483 | brace_gobbler (text, tlen, indx, satisfy) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 484 | char *text; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 485 | size_t tlen; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 486 | int *indx; |
| 487 | int satisfy; |
| 488 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 489 | register int i, c, quoted, level, commas, pass_next; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 490 | #if defined (SHELL) |
| 491 | int si; |
| 492 | char *t; |
| 493 | #endif |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 494 | DECLARE_MBSTATE; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 495 | |
| 496 | level = quoted = pass_next = 0; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 497 | #if defined (CSH_BRACE_COMPAT) |
| 498 | commas = 1; |
| 499 | #else |
| 500 | commas = (satisfy == '}') ? 0 : 1; |
| 501 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 502 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 503 | i = *indx; |
| 504 | while (c = text[i]) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 505 | { |
| 506 | if (pass_next) |
| 507 | { |
| 508 | pass_next = 0; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 509 | ADVANCE_CHAR (text, tlen, i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 510 | continue; |
| 511 | } |
| 512 | |
| 513 | /* A backslash escapes the next character. This allows backslash to |
| 514 | escape the quote character in a double-quoted string. */ |
| 515 | if (c == '\\' && (quoted == 0 || quoted == '"' || quoted == '`')) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 516 | { |
| 517 | pass_next = 1; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 518 | i++; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 519 | continue; |
| 520 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 521 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 522 | #if defined (SHELL) |
| 523 | /* If compiling for the shell, treat ${...} like \{...} */ |
| 524 | if (c == '$' && text[i+1] == '{' && quoted != '\'') /* } */ |
| 525 | { |
| 526 | pass_next = 1; |
| 527 | i++; |
Jari Aalto | eb87367 | 2004-11-09 21:37:25 +0000 | [diff] [blame] | 528 | if (quoted == 0) |
| 529 | level++; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 530 | continue; |
| 531 | } |
| 532 | #endif |
| 533 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 534 | if (quoted) |
| 535 | { |
| 536 | if (c == quoted) |
| 537 | quoted = 0; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 538 | ADVANCE_CHAR (text, tlen, i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 539 | continue; |
| 540 | } |
| 541 | |
| 542 | if (c == '"' || c == '\'' || c == '`') |
| 543 | { |
| 544 | quoted = c; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 545 | i++; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 546 | continue; |
| 547 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 548 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 549 | #if defined (SHELL) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 550 | /* Pass new-style command and process substitutions through unchanged. */ |
| 551 | if ((c == '$' || c == '<' || c == '>') && text[i+1] == '(') /* ) */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 552 | { |
| 553 | si = i + 2; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 554 | t = extract_command_subst (text, &si, 0); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 555 | i = si; |
| 556 | free (t); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 557 | i++; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 558 | continue; |
| 559 | } |
| 560 | #endif |
| 561 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 562 | if (c == satisfy && level == 0 && quoted == 0 && commas > 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 563 | { |
| 564 | /* We ignore an open brace surrounded by whitespace, and also |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 565 | an open brace followed immediately by a close brace preceded |
| 566 | by whitespace. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 567 | if (c == '{' && |
| 568 | ((!i || brace_whitespace (text[i - 1])) && |
| 569 | (brace_whitespace (text[i + 1]) || text[i + 1] == '}'))) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 570 | { |
| 571 | i++; |
| 572 | continue; |
| 573 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 574 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 575 | break; |
| 576 | } |
| 577 | |
| 578 | if (c == '{') |
| 579 | level++; |
| 580 | else if (c == '}' && level) |
| 581 | level--; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 582 | #if !defined (CSH_BRACE_COMPAT) |
| 583 | else if (satisfy == '}' && c == brace_arg_separator && level == 0) |
| 584 | commas++; |
| 585 | else if (satisfy == '}' && STREQN (text+i, BRACE_SEQ_SPECIFIER, 2) && |
| 586 | text[i+2] != satisfy && level == 0) |
| 587 | commas++; |
| 588 | #endif |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 589 | |
| 590 | ADVANCE_CHAR (text, tlen, i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | *indx = i; |
| 594 | return (c); |
| 595 | } |
| 596 | |
| 597 | /* Return a new array of strings which is the result of appending each |
| 598 | string in ARR2 to each string in ARR1. The resultant array is |
| 599 | len (arr1) * len (arr2) long. For convenience, ARR1 (and its contents) |
| 600 | are free ()'ed. ARR1 can be NULL, in that case, a new version of ARR2 |
| 601 | is returned. */ |
| 602 | static char ** |
| 603 | array_concat (arr1, arr2) |
| 604 | char **arr1, **arr2; |
| 605 | { |
| 606 | register int i, j, len, len1, len2; |
| 607 | register char **result; |
| 608 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 609 | if (arr1 == 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 610 | return (strvec_copy (arr2)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 611 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 612 | if (arr2 == 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 613 | return (strvec_copy (arr1)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 614 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 615 | len1 = strvec_len (arr1); |
| 616 | len2 = strvec_len (arr2); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 617 | |
| 618 | result = (char **)xmalloc ((1 + (len1 * len2)) * sizeof (char *)); |
| 619 | |
| 620 | len = 0; |
| 621 | for (i = 0; i < len1; i++) |
| 622 | { |
| 623 | int strlen_1 = strlen (arr1[i]); |
| 624 | |
| 625 | for (j = 0; j < len2; j++) |
| 626 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 627 | result[len] = (char *)xmalloc (1 + strlen_1 + strlen (arr2[j])); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 628 | strcpy (result[len], arr1[i]); |
| 629 | strcpy (result[len] + strlen_1, arr2[j]); |
| 630 | len++; |
| 631 | } |
| 632 | free (arr1[i]); |
| 633 | } |
| 634 | free (arr1); |
| 635 | |
| 636 | result[len] = (char *)NULL; |
| 637 | return (result); |
| 638 | } |
| 639 | |
| 640 | #if defined (TEST) |
| 641 | #include <stdio.h> |
| 642 | |
| 643 | fatal_error (format, arg1, arg2) |
| 644 | char *format, *arg1, *arg2; |
| 645 | { |
| 646 | report_error (format, arg1, arg2); |
| 647 | exit (1); |
| 648 | } |
| 649 | |
| 650 | report_error (format, arg1, arg2) |
| 651 | char *format, *arg1, *arg2; |
| 652 | { |
| 653 | fprintf (stderr, format, arg1, arg2); |
| 654 | fprintf (stderr, "\n"); |
| 655 | } |
| 656 | |
| 657 | main () |
| 658 | { |
| 659 | char example[256]; |
| 660 | |
| 661 | for (;;) |
| 662 | { |
| 663 | char **result; |
| 664 | int i; |
| 665 | |
| 666 | fprintf (stderr, "brace_expand> "); |
| 667 | |
| 668 | if ((!fgets (example, 256, stdin)) || |
| 669 | (strncmp (example, "quit", 4) == 0)) |
| 670 | break; |
| 671 | |
| 672 | if (strlen (example)) |
| 673 | example[strlen (example) - 1] = '\0'; |
| 674 | |
| 675 | result = brace_expand (example); |
| 676 | |
| 677 | for (i = 0; result[i]; i++) |
| 678 | printf ("%s\n", result[i]); |
| 679 | |
| 680 | free_array (result); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Local variables: |
| 686 | * compile-command: "gcc -g -Bstatic -DTEST -o brace_expand braces.c general.o" |
| 687 | * end: |
| 688 | */ |
| 689 | |
| 690 | #endif /* TEST */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 691 | #endif /* BRACE_EXPANSION */ |