From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1UbDcb-00089J-PG for mharc-grub-devel@gnu.org; Sat, 11 May 2013 13:30:13 -0400 Received: from eggs.gnu.org ([208.118.235.92]:36587) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UbDcY-00086F-Me for grub-devel@gnu.org; Sat, 11 May 2013 13:30:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UbDcW-0003dg-4Y for grub-devel@gnu.org; Sat, 11 May 2013 13:30:10 -0400 Received: from mail-lb0-f180.google.com ([209.85.217.180]:48391) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UbDcV-0003dF-PD for grub-devel@gnu.org; Sat, 11 May 2013 13:30:08 -0400 Received: by mail-lb0-f180.google.com with SMTP id v1so5133989lbd.39 for ; Sat, 11 May 2013 10:30:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:date:from:to:subject:message-id:in-reply-to:references :x-mailer:mime-version:content-type; bh=LoyVnTXhjYsWO+YBmfnRdKIWsNyknw1Hg9L/cojLoog=; b=QUftXKqpjUxGFNR9C4IsRSEzBqNraeT+YJYkaDiUXTFA8RWnAnaElv9VJT3wszR72t /UbZH3TuGA2OLBaXquyhAGitQqM6/HfArVMim1ffRnGaNYy6H8W/aUrlDmiVFH0TWmFH x15s57GWfu76BFYCxSWjjUGiPV8iPxACq3JNBGyDY7rSNTg0TJuIrAXWThwvhxXnFry4 AZosgfidfQUNRW3Md4O/0QfTNHht7wuFlrkQHHYph+G3LGTJQgFAKa+qsSn+SOhtaq7K oiYOpl/F1+q/KldI3yq+Ou8o73D+ykfj+a5P3zCHnEq8dl1U+9i/aXIYnO2Y+YTtBYg7 cbNg== X-Received: by 10.112.156.133 with SMTP id we5mr9901953lbb.22.1368293406647; Sat, 11 May 2013 10:30:06 -0700 (PDT) Received: from opensuse.site ([94.29.72.160]) by mx.google.com with ESMTPSA id c5sm1693437lbe.1.2013.05.11.10.30.05 for (version=SSLv3 cipher=RC4-SHA bits=128/128); Sat, 11 May 2013 10:30:06 -0700 (PDT) Date: Sat, 11 May 2013 21:30:05 +0400 From: Andrey Borzenkov To: grub-devel@gnu.org Subject: Re: New command eval. Message-ID: <20130511213005.0827778e@opensuse.site> In-Reply-To: <518E79C2.701@gmail.com> References: <1367240132.58582.YahooMailNeo@web120204.mail.ne1.yahoo.com> <20130501185934.3f2b9c28@opensuse.site> <518578B3.5040003@gmail.com> <20130511203534.52499628@opensuse.site> <518E79C2.701@gmail.com> X-Mailer: Claws Mail 3.9.0 (GTK+ 2.24.14; x86_64-suse-linux-gnu) Mime-Version: 1.0 Content-Type: multipart/signed; micalg=PGP-SHA1; boundary="Sig_/moQHH5esseV2M/hEV7xQD52"; protocol="application/pgp-signature" X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.85.217.180 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 11 May 2013 17:30:12 -0000 --Sig_/moQHH5esseV2M/hEV7xQD52 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable =D0=92 Sat, 11 May 2013 19:02:58 +0200 Vladimir '=CF=86-coder/phcoder' Serbinenko =D0=BF=D0=B8= =D1=88=D0=B5=D1=82: > On 11.05.2013 18:35, Andrey Borzenkov wrote: >=20 > > + grub_size_t size =3D argc; /* +1 for final zero */ > > + char *str, *p; > > + grub_err_t ret; > > + > > + if (argc =3D=3D 0) > > + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("missing arguments")); > > + >=20 > eval with no arguments returns success in bash, to match this behaviour, > it should be GRUB_ERR_NONE and no use of grub_error >=20 > > + for (i =3D 0; i < argc; i++) > > + size +=3D grub_strlen (argv[i]); > > + > > + str =3D p =3D grub_malloc (size); > > + if (!str) > > + return grub_errno; > > + > > + for (i =3D 0; i < argc; i++) > > + { > > + grub_strcpy (p, argv[i]); > > + p +=3D grub_strlen (argv[i]); >=20 > These 2 operations should be condensed by using grub_stpcpy >=20 > > + *p++ =3D ' '; >=20 >=20 > >- grub_script_execute_sourcecode (script, 0, dummy); > >+ grub_script_execute_sourcecode (script); > (in menu_entry.c) here you need a new scope for consistency. >=20 diff --git a/ChangeLog b/ChangeLog index cd8213a..60c64ee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2013-05-11 Andrey Borzenkov + + * 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 =20 * 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 @comman= d{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 =20 =20 +@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 =20 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 =3D { }; =20 module =3D { + name =3D eval; + common =3D commands/eval.c; +}; + +module =3D { name =3D extcmd; common =3D commands/extcmd.c; common =3D 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 . + */ + +#include +#include +#include +#include +#include +#include + +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 =3D argc; /* +1 for final zero */ + char *str, *p; + grub_err_t ret; + + if (argc =3D=3D 0) + return GRUB_ERR_NONE; + + for (i =3D 0; i < argc; i++) + size +=3D grub_strlen (argv[i]); + + str =3D p =3D grub_malloc (size); + if (!str) + return grub_errno; + + for (i =3D 0; i < argc; i++) + { + p =3D grub_stpcpy (p, argv[i]); + *p++ =3D ' '; + } + *--p =3D '\0'; + + ret =3D grub_script_execute_sourcecode (str); + grub_free (str); + return ret; +} + +static grub_command_t cmd; + +GRUB_MOD_INIT(eval) +{ + cmd =3D 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 au= to_boot) else grub_env_unset ("default"); =20 - grub_script_execute_sourcecode (entry->sourcecode, entry->argc, entry->a= rgs); + grub_script_execute_new_scope (entry->sourcecode, entry->argc, entry->ar= gs); =20 if (errs_before !=3D 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] =3D '\0'; } - grub_script_execute_sourcecode (script, 0, dummy); + grub_script_execute_new_scope (script, 0, dummy); grub_free (script); =20 if (errs_before !=3D 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, =20 /* 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 =3D 0; struct grub_script *parsed_script; - struct grub_script_scope new_scope; - struct grub_script_scope *old_scope; - - new_scope.argv.argc =3D argc; - new_scope.argv.args =3D args; - new_scope.flags =3D 0; - - old_scope =3D scope; - scope =3D &new_scope; =20 while (source) { @@ -880,13 +871,35 @@ grub_script_execute_sourcecode (const char *source, i= nt argc, char **args) if (! parsed_script) { ret =3D grub_errno; + grub_free (line); break; } =20 ret =3D grub_script_execute (parsed_script); + grub_script_free (parsed_script); grub_free (line); } =20 + 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 =3D 0; + struct grub_script_scope new_scope; + struct grub_script_scope *old_scope; + + new_scope.argv.argc =3D argc; + new_scope.argv.args =3D args; + new_scope.flags =3D 0; + + old_scope =3D scope; + scope =3D &new_scope; + + ret =3D grub_script_execute_sourcecode (source); + scope =3D 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_sc= ript_cmd *cmd); =20 /* 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, c= har **args); +grub_err_t grub_script_execute_sourcecode (const char *source); +grub_err_t grub_script_execute_new_scope (const char *source, int argc, ch= ar **args); =20 /* Break command for loops. */ grub_err_t grub_script_break (grub_command_t cmd, int argc, char *argv[]); --Sig_/moQHH5esseV2M/hEV7xQD52 Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.19 (GNU/Linux) iEYEARECAAYFAlGOgB0ACgkQR6LMutpd94zu8gCfSl4M12nD2KM/b7J3iFqrdHlf rfoAnjWAD3np8OTGJ44AXc0lTcsRaZeY =H2Tw -----END PGP SIGNATURE----- --Sig_/moQHH5esseV2M/hEV7xQD52--