2010-05-12 04:49:12 +00:00
|
|
|
/* argv.c - methods for constructing argument vector */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2010 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB 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.
|
|
|
|
*
|
|
|
|
* GRUB 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/mm.h>
|
2010-07-29 18:02:56 +00:00
|
|
|
#include <grub/misc.h>
|
2010-05-12 04:49:12 +00:00
|
|
|
#include <grub/script_sh.h>
|
2020-06-15 16:28:27 +00:00
|
|
|
#include <grub/safemath.h>
|
2010-05-12 04:49:12 +00:00
|
|
|
|
2010-07-22 22:35:15 +00:00
|
|
|
/* Return nearest power of two that is >= v. */
|
2010-05-19 04:45:48 +00:00
|
|
|
static unsigned
|
|
|
|
round_up_exp (unsigned v)
|
|
|
|
{
|
2011-01-22 13:11:19 +00:00
|
|
|
COMPILE_TIME_ASSERT (sizeof (v) == 4);
|
|
|
|
|
2010-05-19 04:45:48 +00:00
|
|
|
v--;
|
|
|
|
v |= v >> 1;
|
|
|
|
v |= v >> 2;
|
|
|
|
v |= v >> 4;
|
|
|
|
v |= v >> 8;
|
|
|
|
v |= v >> 16;
|
2010-05-19 04:55:41 +00:00
|
|
|
|
2010-05-19 04:45:48 +00:00
|
|
|
v++;
|
|
|
|
v += (v == 0);
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
2010-05-12 04:49:12 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
grub_script_argv_free (struct grub_script_argv *argv)
|
|
|
|
{
|
2010-05-19 04:55:41 +00:00
|
|
|
unsigned i;
|
2010-05-12 04:49:12 +00:00
|
|
|
|
|
|
|
if (argv->args)
|
|
|
|
{
|
|
|
|
for (i = 0; i < argv->argc; i++)
|
|
|
|
grub_free (argv->args[i]);
|
|
|
|
|
|
|
|
grub_free (argv->args);
|
|
|
|
}
|
|
|
|
|
|
|
|
argv->argc = 0;
|
|
|
|
argv->args = 0;
|
2010-07-21 23:19:05 +00:00
|
|
|
argv->script = 0;
|
2010-05-12 04:49:12 +00:00
|
|
|
}
|
|
|
|
|
2010-08-15 06:32:33 +00:00
|
|
|
/* Make argv from argc, args pair. */
|
|
|
|
int
|
|
|
|
grub_script_argv_make (struct grub_script_argv *argv, int argc, char **args)
|
|
|
|
{
|
|
|
|
int i;
|
2010-09-04 05:26:23 +00:00
|
|
|
struct grub_script_argv r = { 0, 0, 0 };
|
2010-08-15 06:32:33 +00:00
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
2011-11-11 19:34:37 +00:00
|
|
|
if (grub_script_argv_next (&r)
|
|
|
|
|| grub_script_argv_append (&r, args[i], grub_strlen (args[i])))
|
2010-08-15 06:32:33 +00:00
|
|
|
{
|
|
|
|
grub_script_argv_free (&r);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*argv = r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-12 04:49:12 +00:00
|
|
|
/* Prepare for next argc. */
|
|
|
|
int
|
|
|
|
grub_script_argv_next (struct grub_script_argv *argv)
|
|
|
|
{
|
|
|
|
char **p = argv->args;
|
2020-06-15 16:28:27 +00:00
|
|
|
grub_size_t sz;
|
2010-05-12 04:49:12 +00:00
|
|
|
|
2010-06-10 06:42:03 +00:00
|
|
|
if (argv->args && argv->argc && argv->args[argv->argc - 1] == 0)
|
2010-05-12 04:49:12 +00:00
|
|
|
return 0;
|
|
|
|
|
2020-06-15 16:28:27 +00:00
|
|
|
if (grub_add (argv->argc, 2, &sz) ||
|
|
|
|
grub_mul (sz, sizeof (char *), &sz))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
p = grub_realloc (p, round_up_exp (sz));
|
2010-05-12 04:49:12 +00:00
|
|
|
if (! p)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
argv->argc++;
|
|
|
|
argv->args = p;
|
2010-05-21 10:04:36 +00:00
|
|
|
|
|
|
|
if (argv->argc == 1)
|
|
|
|
argv->args[0] = 0;
|
2010-05-12 04:49:12 +00:00
|
|
|
argv->args[argv->argc] = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-29 17:22:09 +00:00
|
|
|
/* Append `s' to the last argument. */
|
|
|
|
int
|
2011-11-11 19:34:37 +00:00
|
|
|
grub_script_argv_append (struct grub_script_argv *argv, const char *s,
|
|
|
|
grub_size_t slen)
|
2010-05-12 04:49:12 +00:00
|
|
|
{
|
2011-11-11 19:34:37 +00:00
|
|
|
grub_size_t a;
|
2010-05-12 04:49:12 +00:00
|
|
|
char *p = argv->args[argv->argc - 1];
|
2020-06-15 16:28:27 +00:00
|
|
|
grub_size_t sz;
|
2010-05-12 04:49:12 +00:00
|
|
|
|
|
|
|
if (! s)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
a = p ? grub_strlen (p) : 0;
|
|
|
|
|
2020-06-15 16:28:27 +00:00
|
|
|
if (grub_add (a, slen, &sz) ||
|
|
|
|
grub_add (sz, 1, &sz) ||
|
|
|
|
grub_mul (sz, sizeof (char), &sz))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
p = grub_realloc (p, round_up_exp (sz));
|
2010-05-12 04:49:12 +00:00
|
|
|
if (! p)
|
|
|
|
return 1;
|
|
|
|
|
2011-11-11 19:34:37 +00:00
|
|
|
grub_memcpy (p + a, s, slen);
|
|
|
|
p[a+slen] = 0;
|
2010-05-12 04:49:12 +00:00
|
|
|
argv->args[argv->argc - 1] = p;
|
2010-07-29 15:39:18 +00:00
|
|
|
|
2010-05-12 04:49:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Split `s' and append words as multiple arguments. */
|
|
|
|
int
|
2011-11-11 19:34:37 +00:00
|
|
|
grub_script_argv_split_append (struct grub_script_argv *argv, const char *s)
|
2010-05-12 04:49:12 +00:00
|
|
|
{
|
2011-11-11 19:34:37 +00:00
|
|
|
const char *p;
|
2010-05-12 04:49:12 +00:00
|
|
|
int errors = 0;
|
|
|
|
|
|
|
|
if (! s)
|
|
|
|
return 0;
|
|
|
|
|
2012-03-19 12:29:43 +00:00
|
|
|
while (*s && grub_isspace (*s))
|
|
|
|
s++;
|
|
|
|
|
2010-05-12 04:49:12 +00:00
|
|
|
while (! errors && *s)
|
|
|
|
{
|
|
|
|
p = s;
|
|
|
|
while (*s && ! grub_isspace (*s))
|
|
|
|
s++;
|
|
|
|
|
2011-11-11 19:34:37 +00:00
|
|
|
errors += grub_script_argv_append (argv, p, s - p);
|
2010-05-12 04:49:12 +00:00
|
|
|
|
|
|
|
while (*s && grub_isspace (*s))
|
|
|
|
s++;
|
|
|
|
|
|
|
|
if (*s)
|
|
|
|
errors += grub_script_argv_next (argv);
|
|
|
|
}
|
|
|
|
return errors;
|
|
|
|
}
|