Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / wildcard.c
blobd207acfed700091817b3972268cd6eb934673104
1 /* wildcard.c - Wildcard character expansion for GRUB script. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2010 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/mm.h>
21 #include <grub/fs.h>
22 #include <grub/env.h>
23 #include <grub/file.h>
24 #include <grub/device.h>
25 #include <grub/script_sh.h>
27 #include <regex.h>
29 static inline int isregexop (char ch);
30 static char ** merge (char **lhs, char **rhs);
31 static char *make_dir (const char *prefix, const char *start, const char *end);
32 static int make_regex (const char *regex_start, const char *regex_end,
33 regex_t *regexp);
34 static void split_path (const char *path, const char **suffix_end, const char **regex_end);
35 static char ** match_devices (const regex_t *regexp, int noparts);
36 static char ** match_files (const char *prefix, const char *suffix_start,
37 const char *suffix_end, const regex_t *regexp);
39 static grub_err_t wildcard_expand (const char *s, char ***strs);
41 struct grub_script_wildcard_translator grub_filename_translator = {
42 .expand = wildcard_expand,
45 static char **
46 merge (char **dest, char **ps)
48 int i;
49 int j;
50 char **p;
52 if (! dest)
53 return ps;
55 if (! ps)
56 return dest;
58 for (i = 0; dest[i]; i++)
60 for (j = 0; ps[j]; j++)
63 p = grub_realloc (dest, sizeof (char*) * (i + j + 1));
64 if (! p)
66 grub_free (dest);
67 grub_free (ps);
68 return 0;
71 dest = p;
72 for (j = 0; ps[j]; j++)
73 dest[i++] = ps[j];
74 dest[i] = 0;
76 grub_free (ps);
77 return dest;
80 static inline int
81 isregexop (char ch)
83 return grub_strchr ("*.\\|+{}[]?", ch) ? 1 : 0;
86 static char *
87 make_dir (const char *prefix, const char *start, const char *end)
89 char ch;
90 unsigned i;
91 unsigned n;
92 char *result;
94 i = grub_strlen (prefix);
95 n = i + end - start;
97 result = grub_malloc (n + 1);
98 if (! result)
99 return 0;
101 grub_strcpy (result, prefix);
102 while (start < end && (ch = *start++))
103 if (ch == '\\' && isregexop (*start))
104 result[i++] = *start++;
105 else
106 result[i++] = ch;
108 result[i] = '\0';
109 return result;
112 static int
113 make_regex (const char *start, const char *end, regex_t *regexp)
115 char ch;
116 int i = 0;
117 unsigned len = end - start;
118 char *buffer = grub_malloc (len * 2 + 2 + 1); /* worst case size. */
120 if (! buffer)
121 return 1;
123 buffer[i++] = '^';
124 while (start < end)
126 /* XXX Only * and ? expansion for now. */
127 switch ((ch = *start++))
129 case '\\':
130 buffer[i++] = ch;
131 if (*start != '\0')
132 buffer[i++] = *start++;
133 break;
135 case '.':
136 case '(':
137 case ')':
138 case '@':
139 case '+':
140 case '|':
141 case '{':
142 case '}':
143 case '[':
144 case ']':
145 buffer[i++] = '\\';
146 buffer[i++] = ch;
147 break;
149 case '*':
150 buffer[i++] = '.';
151 buffer[i++] = '*';
152 break;
154 case '?':
155 buffer[i++] = '.';
156 break;
158 default:
159 buffer[i++] = ch;
162 buffer[i++] = '$';
163 buffer[i] = '\0';
164 grub_dprintf ("expand", "Regexp is %s\n", buffer);
166 if (regcomp (regexp, buffer, RE_SYNTAX_GNU_AWK))
168 grub_free (buffer);
169 return 1;
172 grub_free (buffer);
173 return 0;
176 /* Split `str' into two parts: (1) dirname that is regexop free (2)
177 dirname that has a regexop. */
178 static void
179 split_path (const char *str, const char **noregexop, const char **regexop)
181 char ch = 0;
182 int regex = 0;
184 const char *end;
185 const char *split; /* points till the end of dirnaname that doesn't
186 need expansion. */
188 split = end = str;
189 while ((ch = *end))
191 if (ch == '\\' && end[1])
192 end++;
194 else if (ch == '*' || ch == '?')
195 regex = 1;
197 else if (ch == '/' && ! regex)
198 split = end + 1; /* forward to next regexop-free dirname */
200 else if (ch == '/' && regex)
201 break; /* stop at the first dirname with a regexop */
203 end++;
206 *regexop = end;
207 if (! regex)
208 *noregexop = end;
209 else
210 *noregexop = split;
213 /* Context for match_devices. */
214 struct match_devices_ctx
216 const regex_t *regexp;
217 int noparts;
218 int ndev;
219 char **devs;
222 /* Helper for match_devices. */
223 static int
224 match_devices_iter (const char *name, void *data)
226 struct match_devices_ctx *ctx = data;
227 char **t;
228 char *buffer;
230 /* skip partitions if asked to. */
231 if (ctx->noparts && grub_strchr (name, ','))
232 return 0;
234 buffer = grub_xasprintf ("(%s)", name);
235 if (! buffer)
236 return 1;
238 grub_dprintf ("expand", "matching: %s\n", buffer);
239 if (regexec (ctx->regexp, buffer, 0, 0, 0))
241 grub_dprintf ("expand", "not matched\n");
242 grub_free (buffer);
243 return 0;
246 t = grub_realloc (ctx->devs, sizeof (char*) * (ctx->ndev + 2));
247 if (! t)
249 grub_free (buffer);
250 return 1;
253 ctx->devs = t;
254 ctx->devs[ctx->ndev++] = buffer;
255 ctx->devs[ctx->ndev] = 0;
256 return 0;
259 static char **
260 match_devices (const regex_t *regexp, int noparts)
262 struct match_devices_ctx ctx = {
263 .regexp = regexp,
264 .noparts = noparts,
265 .ndev = 0,
266 .devs = 0
268 int i;
270 if (grub_device_iterate (match_devices_iter, &ctx))
271 goto fail;
273 return ctx.devs;
275 fail:
277 for (i = 0; ctx.devs && ctx.devs[i]; i++)
278 grub_free (ctx.devs[i]);
280 grub_free (ctx.devs);
282 return 0;
285 /* Context for match_files. */
286 struct match_files_ctx
288 const regex_t *regexp;
289 char **files;
290 unsigned nfile;
291 char *dir;
294 /* Helper for match_files. */
295 static int
296 match_files_iter (const char *name, const struct grub_dirhook_info *info,
297 void *data)
299 struct match_files_ctx *ctx = data;
300 char **t;
301 char *buffer;
303 /* skip . and .. names */
304 if (grub_strcmp(".", name) == 0 || grub_strcmp("..", name) == 0)
305 return 0;
307 grub_dprintf ("expand", "matching: %s in %s\n", name, ctx->dir);
308 if (regexec (ctx->regexp, name, 0, 0, 0))
309 return 0;
311 grub_dprintf ("expand", "matched\n");
313 buffer = grub_xasprintf ("%s%s", ctx->dir, name);
314 if (! buffer)
315 return 1;
317 t = grub_realloc (ctx->files, sizeof (char*) * (ctx->nfile + 2));
318 if (! t)
320 grub_free (buffer);
321 return 1;
324 ctx->files = t;
325 ctx->files[ctx->nfile++] = buffer;
326 ctx->files[ctx->nfile] = 0;
327 return 0;
330 static char **
331 match_files (const char *prefix, const char *suffix, const char *end,
332 const regex_t *regexp)
334 struct match_files_ctx ctx = {
335 .regexp = regexp,
336 .nfile = 0,
337 .files = 0
339 int i;
340 const char *path;
341 char *device_name;
342 grub_fs_t fs;
343 grub_device_t dev;
345 dev = 0;
346 device_name = 0;
347 grub_error_push ();
349 ctx.dir = make_dir (prefix, suffix, end);
350 if (! ctx.dir)
351 goto fail;
353 device_name = grub_file_get_device_name (ctx.dir);
354 dev = grub_device_open (device_name);
355 if (! dev)
356 goto fail;
358 fs = grub_fs_probe (dev);
359 if (! fs)
360 goto fail;
362 if (ctx.dir[0] == '(')
364 path = grub_strchr (ctx.dir, ')');
365 if (!path)
366 goto fail;
367 path++;
369 else
370 path = ctx.dir;
372 if (fs->dir (dev, path, match_files_iter, &ctx))
373 goto fail;
375 grub_free (ctx.dir);
376 grub_device_close (dev);
377 grub_free (device_name);
378 grub_error_pop ();
379 return ctx.files;
381 fail:
383 grub_free (ctx.dir);
385 for (i = 0; ctx.files && ctx.files[i]; i++)
386 grub_free (ctx.files[i]);
388 grub_free (ctx.files);
390 if (dev)
391 grub_device_close (dev);
393 grub_free (device_name);
395 grub_error_pop ();
396 return 0;
399 /* Context for check_file. */
400 struct check_file_ctx
402 const char *basename;
403 int found;
406 /* Helper for check_file. */
407 static int
408 check_file_iter (const char *name, const struct grub_dirhook_info *info,
409 void *data)
411 struct check_file_ctx *ctx = data;
413 if (ctx->basename[0] == 0
414 || (info->case_insensitive ? grub_strcasecmp (name, ctx->basename) == 0
415 : grub_strcmp (name, ctx->basename) == 0))
417 ctx->found = 1;
418 return 1;
421 return 0;
424 static int
425 check_file (const char *dir, const char *basename)
427 struct check_file_ctx ctx = {
428 .basename = basename,
429 .found = 0
431 grub_fs_t fs;
432 grub_device_t dev;
433 const char *device_name, *path;
435 device_name = grub_file_get_device_name (dir);
436 dev = grub_device_open (device_name);
437 if (! dev)
438 goto fail;
440 fs = grub_fs_probe (dev);
441 if (! fs)
442 goto fail;
444 if (dir[0] == '(')
446 path = grub_strchr (dir, ')');
447 if (!path)
448 goto fail;
449 path++;
451 else
452 path = dir;
454 fs->dir (dev, path[0] ? path : "/", check_file_iter, &ctx);
455 if (grub_errno == 0 && basename[0] == 0)
456 ctx.found = 1;
458 fail:
459 grub_errno = 0;
461 return ctx.found;
464 static void
465 unescape (char *out, const char *in, const char *end)
467 char *optr;
468 const char *iptr;
470 for (optr = out, iptr = in; iptr < end;)
472 if (*iptr == '\\' && iptr + 1 < end)
474 *optr++ = iptr[1];
475 iptr += 2;
476 continue;
478 if (*iptr == '\\')
479 break;
480 *optr++ = *iptr++;
482 *optr = 0;
485 static grub_err_t
486 wildcard_expand (const char *s, char ***strs)
488 const char *start;
489 const char *regexop;
490 const char *noregexop;
491 char **paths = 0;
492 int had_regexp = 0;
494 unsigned i;
495 regex_t regexp;
497 *strs = 0;
498 if (s[0] != '/' && s[0] != '(' && s[0] != '*')
499 return 0;
501 start = s;
502 while (*start)
504 split_path (start, &noregexop, &regexop);
506 if (noregexop == regexop)
508 grub_dprintf ("expand", "no expansion needed\n");
509 if (paths == 0)
511 paths = grub_malloc (sizeof (char *) * 2);
512 if (!paths)
513 goto fail;
514 paths[0] = grub_malloc (regexop - start + 1);
515 if (!paths[0])
516 goto fail;
517 unescape (paths[0], start, regexop);
518 paths[1] = 0;
520 else
522 int j = 0;
523 for (i = 0; paths[i]; i++)
525 char *o, *oend;
526 char *n;
527 char *p;
528 o = paths[i];
529 oend = o + grub_strlen (o);
530 n = grub_malloc ((oend - o) + (regexop - start) + 1);
531 if (!n)
532 goto fail;
533 grub_memcpy (n, o, oend - o);
535 unescape (n + (oend - o), start, regexop);
536 if (had_regexp)
537 p = grub_strrchr (n, '/');
538 else
539 p = 0;
540 if (!p)
542 grub_free (o);
543 paths[j++] = n;
544 continue;
546 *p = 0;
547 if (!check_file (n, p + 1))
549 grub_dprintf ("expand", "file <%s> in <%s> not found\n",
550 p + 1, n);
551 grub_free (o);
552 grub_free (n);
553 continue;
555 *p = '/';
556 grub_free (o);
557 paths[j++] = n;
559 if (j == 0)
561 grub_free (paths);
562 paths = 0;
563 goto done;
565 paths[j] = 0;
567 grub_dprintf ("expand", "paths[0] = `%s'\n", paths[0]);
568 start = regexop;
569 continue;
572 if (make_regex (noregexop, regexop, &regexp))
573 goto fail;
575 had_regexp = 1;
577 if (paths == 0)
579 if (start == noregexop) /* device part has regexop */
580 paths = match_devices (&regexp, *start != '(');
582 else /* device part explicit wo regexop */
583 paths = match_files ("", start, noregexop, &regexp);
585 else
587 char **r = 0;
589 for (i = 0; paths[i]; i++)
591 char **p;
593 p = match_files (paths[i], start, noregexop, &regexp);
594 grub_free (paths[i]);
595 if (! p)
596 continue;
598 r = merge (r, p);
599 if (! r)
600 goto fail;
602 grub_free (paths);
603 paths = r;
606 regfree (&regexp);
607 if (! paths)
608 goto done;
610 start = regexop;
613 done:
615 *strs = paths;
616 return 0;
618 fail:
620 for (i = 0; paths && paths[i]; i++)
621 grub_free (paths[i]);
622 grub_free (paths);
623 regfree (&regexp);
624 return grub_errno;