grub.cfg 中支持if/else等语句,这些命令是通过c code来解析的,我们以if为例
看看grub2中是如何用c语言解析if语句的.
在grub 中if语句的处理如下
/* Execute an if statement. */
grub_err_t
grub_script_execute_cmdif (struct grub_script_cmd *cmd)
{
int ret;
const char *result;
struct grub_script_cmdif *cmdif = (struct grub_script_cmdif *) cmd;
/* Check if the commands results in a true or a false. The value is
read from the env variable `?'. */
ret = grub_script_execute_cmd (cmdif->exec_to_evaluate);
if (function_return)
return ret;
result = grub_env_get ("?");
grub_errno = GRUB_ERR_NONE;
/* Execute the `if' or the `else' part depending on the value of
`?'. */
if (result && ! grub_strcmp (result, "0"))
return grub_script_execute_cmd (cmdif->exec_on_true);
else
return grub_script_execute_cmd (cmdif->exec_on_false);
}
这个函数首先通过grub_script_execute_cmd来执行if的判断语句
然后通过grub_env_get来得到结果
如果if的条件成立的话就执行grub_script_execute_cmd (cmdif->exec_on_true);
否则就执行grub_script_execute_cmd (cmdif->exec_on_false);
而
static grub_err_t
grub_script_execute_cmd (struct grub_script_cmd *cmd)
{
int ret;
char errnobuf[ERRNO_DIGITS_MAX + 1];
if (cmd == 0)
return 0;
ret = cmd->exec (cmd);
grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret);
grub_env_set ("?", errnobuf);
return ret;
}
调用cmd本身的exec函数来执行,并将结果通过grub_env_set保存在?中。
/* An if statement. */
struct grub_script_cmdif
{
struct grub_script_cmd cmd;
/* The command used to check if the 'if' is true or false. */
struct grub_script_cmd *exec_to_evaluate;
/* The code executed in case the result of 'if' was true. */
struct grub_script_cmd *exec_on_true;
/* The code executed in case the result of 'if' was false. */
struct grub_script_cmd *exec_on_false;
}
这个结构体也是专门为if语句设置,可以看到每一项都是grub_script_cmd且分为if语句执行
成功和失败的case.
如果在grub.cfg中检测到if语句就会调用grub_script_execute_cmdif。而这时通过下面的函数设定的
struct grub_script_cmd *
grub_script_create_cmdif (struct grub_parser_param *state,
struct grub_script_cmd *exec_to_evaluate,
struct grub_script_cmd *exec_on_true,
struct grub_script_cmd *exec_on_false)
{
struct grub_script_cmdif *cmd;
grub_dprintf ("scripting", "cmdif\n");
cmd = grub_script_malloc (state, sizeof (*cmd));
if (!cmd)
return 0;
cmd->cmd.exec = grub_script_execute_cmdif;
cmd->cmd.next = 0;
cmd->exec_to_evaluate = exec_to_evaluate;
cmd->exec_on_true = exec_on_true;
cmd->exec_on_false = exec_on_false;
return (struct grub_script_cmd *) cmd;
}
这些cmd。if/for等会组成一个list.
看看grub2中是如何用c语言解析if语句的.
在grub 中if语句的处理如下
/* Execute an if statement. */
grub_err_t
grub_script_execute_cmdif (struct grub_script_cmd *cmd)
{
int ret;
const char *result;
struct grub_script_cmdif *cmdif = (struct grub_script_cmdif *) cmd;
/* Check if the commands results in a true or a false. The value is
read from the env variable `?'. */
ret = grub_script_execute_cmd (cmdif->exec_to_evaluate);
if (function_return)
return ret;
result = grub_env_get ("?");
grub_errno = GRUB_ERR_NONE;
/* Execute the `if' or the `else' part depending on the value of
`?'. */
if (result && ! grub_strcmp (result, "0"))
return grub_script_execute_cmd (cmdif->exec_on_true);
else
return grub_script_execute_cmd (cmdif->exec_on_false);
}
这个函数首先通过grub_script_execute_cmd来执行if的判断语句
然后通过grub_env_get来得到结果
如果if的条件成立的话就执行grub_script_execute_cmd (cmdif->exec_on_true);
否则就执行grub_script_execute_cmd (cmdif->exec_on_false);
而
static grub_err_t
grub_script_execute_cmd (struct grub_script_cmd *cmd)
{
int ret;
char errnobuf[ERRNO_DIGITS_MAX + 1];
if (cmd == 0)
return 0;
ret = cmd->exec (cmd);
grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret);
grub_env_set ("?", errnobuf);
return ret;
}
调用cmd本身的exec函数来执行,并将结果通过grub_env_set保存在?中。
/* An if statement. */
struct grub_script_cmdif
{
struct grub_script_cmd cmd;
/* The command used to check if the 'if' is true or false. */
struct grub_script_cmd *exec_to_evaluate;
/* The code executed in case the result of 'if' was true. */
struct grub_script_cmd *exec_on_true;
/* The code executed in case the result of 'if' was false. */
struct grub_script_cmd *exec_on_false;
}
这个结构体也是专门为if语句设置,可以看到每一项都是grub_script_cmd且分为if语句执行
成功和失败的case.
如果在grub.cfg中检测到if语句就会调用grub_script_execute_cmdif。而这时通过下面的函数设定的
struct grub_script_cmd *
grub_script_create_cmdif (struct grub_parser_param *state,
struct grub_script_cmd *exec_to_evaluate,
struct grub_script_cmd *exec_on_true,
struct grub_script_cmd *exec_on_false)
{
struct grub_script_cmdif *cmd;
grub_dprintf ("scripting", "cmdif\n");
cmd = grub_script_malloc (state, sizeof (*cmd));
if (!cmd)
return 0;
cmd->cmd.exec = grub_script_execute_cmdif;
cmd->cmd.next = 0;
cmd->exec_to_evaluate = exec_to_evaluate;
cmd->exec_on_true = exec_on_true;
cmd->exec_on_false = exec_on_false;
return (struct grub_script_cmd *) cmd;
}
这些cmd。if/for等会组成一个list.