From: Andrey Borzenkov <arvidjaar@gmail.com>
To: grub-devel@gnu.org
Subject: Re: New command eval.
Date: Sat, 11 May 2013 21:30:05 +0400 [thread overview]
Message-ID: <20130511213005.0827778e@opensuse.site> (raw)
In-Reply-To: <518E79C2.701@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 8357 bytes --]
В Sat, 11 May 2013 19:02:58 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <phcoder@gmail.com> пишет:
> On 11.05.2013 18:35, Andrey Borzenkov wrote:
>
> > + grub_size_t size = argc; /* +1 for final zero */
> > + char *str, *p;
> > + grub_err_t ret;
> > +
> > + if (argc == 0)
> > + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing arguments"));
> > +
>
> eval with no arguments returns success in bash, to match this behaviour,
> it should be GRUB_ERR_NONE and no use of grub_error
>
> > + for (i = 0; i < argc; i++)
> > + size += grub_strlen (argv[i]);
> > +
> > + str = p = grub_malloc (size);
> > + if (!str)
> > + return grub_errno;
> > +
> > + for (i = 0; i < argc; i++)
> > + {
> > + grub_strcpy (p, argv[i]);
> > + p += grub_strlen (argv[i]);
>
> These 2 operations should be condensed by using grub_stpcpy
>
> > + *p++ = ' ';
>
>
> >- grub_script_execute_sourcecode (script, 0, dummy);
> >+ grub_script_execute_sourcecode (script);
> (in menu_entry.c) here you need a new scope for consistency.
>
diff --git a/ChangeLog b/ChangeLog
index cd8213a..60c64ee 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2013-05-11 Andrey Borzenkov <arvidjaar@gmail.com>
+
+ * grub-core/script/execute.c (grub_script_execute_sourcecode): Split
+ off new function grub_script_execute_new_scope. Change callers to use
+ either of them as appropriate.
+ * grub-core/commands/eval.c: New command eval.
+ * docs/grub.texi (Commands): Document it.
+
2013-05-11 Vladimir Serbinenko <phcoder@gmail.com>
* util/grub-install.in: Gettextize "Not found" message.
diff --git a/docs/grub.texi b/docs/grub.texi
index 75a5ea4..9dcfa2d 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -3435,6 +3435,7 @@ you forget a command, you can run the command @command{help}
* date:: Display or set current date and time
* drivemap:: Map a drive to another
* echo:: Display a line of text
+* eval:: Evaluate agruments as GRUB commands
* export:: Export an environment variable
* false:: Do nothing, unsuccessfully
* gettext:: Translate a string
@@ -3812,6 +3813,15 @@ character will print that character.
@end deffn
+@node eval
+@subsection eval
+
+@deffn Command eval string ...
+Concatenate arguments together using single space as separator and evaluate
+result as sequence of GRUB commands.
+@end deffn
+
+
@node export
@subsection export
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 602b497..0fcc4e7 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -699,6 +699,11 @@ module = {
};
module = {
+ name = eval;
+ common = commands/eval.c;
+};
+
+module = {
name = extcmd;
common = commands/extcmd.c;
common = lib/arg.c;
diff --git a/grub-core/commands/eval.c b/grub-core/commands/eval.c
new file mode 100644
index 0000000..f826a4f
--- /dev/null
+++ b/grub-core/commands/eval.c
@@ -0,0 +1,71 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2009 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/dl.h>
+#include <grub/misc.h>
+#include <grub/script_sh.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+#include <grub/term.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_err_t
+grub_cmd_eval (grub_command_t cmd __attribute__((__unused__)),
+ int argc, char *argv[])
+{
+ int i;
+ grub_size_t size = argc; /* +1 for final zero */
+ char *str, *p;
+ grub_err_t ret;
+
+ if (argc == 0)
+ return GRUB_ERR_NONE;
+
+ for (i = 0; i < argc; i++)
+ size += grub_strlen (argv[i]);
+
+ str = p = grub_malloc (size);
+ if (!str)
+ return grub_errno;
+
+ for (i = 0; i < argc; i++)
+ {
+ p = grub_stpcpy (p, argv[i]);
+ *p++ = ' ';
+ }
+ *--p = '\0';
+
+ ret = grub_script_execute_sourcecode (str);
+ grub_free (str);
+ return ret;
+}
+
+static grub_command_t cmd;
+
+GRUB_MOD_INIT(eval)
+{
+ cmd = grub_register_command ("eval", grub_cmd_eval, N_("STRING ..."),
+ N_("Evaluate arguments as GRUB commands"));
+}
+
+GRUB_MOD_FINI(eval)
+{
+ grub_unregister_command (cmd);
+}
+
diff --git a/grub-core/normal/menu.c b/grub-core/normal/menu.c
index fba19db..9b88290 100644
--- a/grub-core/normal/menu.c
+++ b/grub-core/normal/menu.c
@@ -245,7 +245,7 @@ grub_menu_execute_entry(grub_menu_entry_t entry, int auto_boot)
else
grub_env_unset ("default");
- grub_script_execute_sourcecode (entry->sourcecode, entry->argc, entry->args);
+ grub_script_execute_new_scope (entry->sourcecode, entry->argc, entry->args);
if (errs_before != grub_err_printed_errors)
grub_wait_after_message ();
diff --git a/grub-core/normal/menu_entry.c b/grub-core/normal/menu_entry.c
index fae258a..506715c 100644
--- a/grub-core/normal/menu_entry.c
+++ b/grub-core/normal/menu_entry.c
@@ -1160,7 +1160,7 @@ run (struct screen *screen)
}
script[size] = '\0';
}
- grub_script_execute_sourcecode (script, 0, dummy);
+ grub_script_execute_new_scope (script, 0, dummy);
grub_free (script);
if (errs_before != grub_err_printed_errors)
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
index 9babbee..afd5513 100644
--- a/grub-core/script/execute.c
+++ b/grub-core/script/execute.c
@@ -856,19 +856,10 @@ grub_script_execute_sourcecode_getline (char **line,
/* Execute a source script. */
grub_err_t
-grub_script_execute_sourcecode (const char *source, int argc, char **args)
+grub_script_execute_sourcecode (const char *source)
{
grub_err_t ret = 0;
struct grub_script *parsed_script;
- struct grub_script_scope new_scope;
- struct grub_script_scope *old_scope;
-
- new_scope.argv.argc = argc;
- new_scope.argv.args = args;
- new_scope.flags = 0;
-
- old_scope = scope;
- scope = &new_scope;
while (source)
{
@@ -880,13 +871,35 @@ grub_script_execute_sourcecode (const char *source, int argc, char **args)
if (! parsed_script)
{
ret = grub_errno;
+ grub_free (line);
break;
}
ret = grub_script_execute (parsed_script);
+ grub_script_free (parsed_script);
grub_free (line);
}
+ return ret;
+}
+
+/* Execute a source script in new scope. */
+grub_err_t
+grub_script_execute_new_scope (const char *source, int argc, char **args)
+{
+ grub_err_t ret = 0;
+ struct grub_script_scope new_scope;
+ struct grub_script_scope *old_scope;
+
+ new_scope.argv.argc = argc;
+ new_scope.argv.args = args;
+ new_scope.flags = 0;
+
+ old_scope = scope;
+ scope = &new_scope;
+
+ ret = grub_script_execute_sourcecode (source);
+
scope = old_scope;
return ret;
}
diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h
index 78602e4..57bdd4d 100644
--- a/include/grub/script_sh.h
+++ b/include/grub/script_sh.h
@@ -329,7 +329,8 @@ grub_err_t grub_script_execute_cmdwhile (struct grub_script_cmd *cmd);
/* Execute any GRUB pre-parsed command or script. */
grub_err_t grub_script_execute (struct grub_script *script);
-grub_err_t grub_script_execute_sourcecode (const char *source, int argc, char **args);
+grub_err_t grub_script_execute_sourcecode (const char *source);
+grub_err_t grub_script_execute_new_scope (const char *source, int argc, char **args);
/* Break command for loops. */
grub_err_t grub_script_break (grub_command_t cmd, int argc, char *argv[]);
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
next prev parent reply other threads:[~2013-05-11 17:30 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1367240132.58582.YahooMailNeo@web120204.mail.ne1.yahoo.com>
2013-05-01 14:59 ` Obtaining the MAC address of the boot NIC for a PXE boot Andrey Borzenkov
2013-05-04 21:08 ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-05-04 21:19 ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-05-05 5:17 ` Andrey Borzenkov
2013-05-07 10:03 ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-05-07 17:50 ` Documentation for (Re: Obtaining the MAC address of the boot NIC for a PXE boot) Andrey Borzenkov
2013-05-07 19:45 ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-05-08 14:52 ` Andrey Borzenkov
2013-05-11 16:35 ` New command eval Andrey Borzenkov
2013-05-11 17:02 ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-05-11 17:30 ` Andrey Borzenkov [this message]
2013-05-14 6:26 ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-05-12 0:07 ` Seth Goldberg
2013-05-12 5:39 ` Andrey Borzenkov
2013-05-13 5:13 ` Seth Goldberg
2013-05-14 7:21 ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-05-08 17:27 ` Obtaining the MAC address of the boot NIC for a PXE boot Rigoberto Corujo
2013-05-08 19:25 ` Andrey Borzenkov
2013-05-10 16:12 ` Rigoberto Corujo
2013-05-11 4:47 ` Andrey Borzenkov
2013-05-15 17:09 ` Rigoberto Corujo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130511213005.0827778e@opensuse.site \
--to=arvidjaar@gmail.com \
--cc=grub-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).