grub.cfg中if语句解析

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值