~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to normal/execute.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* execute.c -- Execute a GRUB script.  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2005  Free Software Foundation, Inc.
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#include <grub/misc.h>
 
22
#include <grub/mm.h>
 
23
#include <grub/normal.h>
 
24
#include <grub/arg.h>
 
25
#include <grub/env.h>
 
26
#include <grub/script.h>
 
27
 
 
28
static int
 
29
grub_script_execute_cmd (struct grub_script_cmd *cmd)
 
30
{
 
31
  if (cmd == 0)
 
32
    return 0;
 
33
  cmd->exec (cmd);
 
34
 
 
35
  return 0;
 
36
}
 
37
 
 
38
/* Parse ARG and return the textual representation.  Add strings are
 
39
   concatenated and all values of the variables are filled in.  */
 
40
static char *
 
41
grub_script_execute_argument_to_string (struct grub_script_arg *arg)
 
42
{
 
43
  int size = 0;
 
44
  char *val;
 
45
  char *chararg;
 
46
  struct grub_script_arg *argi;
 
47
 
 
48
  /* First determine the size of the argument.  */
 
49
  for (argi = arg; argi; argi = argi->next)
 
50
    {
 
51
      if (argi->type == 1)
 
52
        {
 
53
          val = grub_env_get (argi->str);
 
54
          size += grub_strlen (val);
 
55
        }
 
56
      else
 
57
        size += grub_strlen (argi->str);
 
58
    }
 
59
 
 
60
  /* Create the argument.  */
 
61
  chararg = grub_malloc (size + 1);
 
62
  if (! chararg)
 
63
    return 0;
 
64
 
 
65
  *chararg = '\0';
 
66
  /* First determine the size of the argument.  */
 
67
  for (argi = arg; argi; argi = argi->next)
 
68
    {
 
69
      if (argi->type == 1)
 
70
        {
 
71
          val = grub_env_get (argi->str);
 
72
          grub_strcat (chararg, val);
 
73
        }
 
74
      else
 
75
        grub_strcat (chararg, argi->str);
 
76
    }
 
77
 
 
78
  return chararg;
 
79
}
 
80
 
 
81
/* Execute a single command line.  */
 
82
grub_err_t
 
83
grub_script_execute_cmdline (struct grub_script_cmd *cmd)
 
84
{
 
85
  struct grub_script_cmdline *cmdline = (struct grub_script_cmdline *) cmd;
 
86
  struct grub_script_arglist *arglist;
 
87
  char **args = 0;
 
88
  int i = 0;
 
89
  grub_command_t grubcmd;
 
90
  struct grub_arg_list *state;
 
91
  struct grub_arg_option *parser;
 
92
  int maxargs = 0;
 
93
  char **parsed_arglist;
 
94
  int numargs;
 
95
  grub_err_t ret = 0;
 
96
  int argcount = 0;
 
97
  grub_script_function_t func = 0;
 
98
  char errnobuf[6];
 
99
 
 
100
  /* Lookup the command.  */
 
101
  grubcmd = grub_command_find (cmdline->cmdname);
 
102
  if (! grubcmd)
 
103
    {
 
104
      /* It's not a GRUB command, try all functions.  */
 
105
      func = grub_script_function_find (cmdline->cmdname);
 
106
      if (! func)
 
107
        return 0;
 
108
    }
 
109
 
 
110
  if (cmdline->arglist)
 
111
    {
 
112
      argcount = cmdline->arglist->argcount;
 
113
 
 
114
      /* Create argv from the arguments.  */
 
115
      args = grub_malloc (sizeof (char *) * argcount);
 
116
      for (arglist = cmdline->arglist; arglist; arglist = arglist->next)
 
117
        {
 
118
          char *str;
 
119
          str = grub_script_execute_argument_to_string (arglist->arg);
 
120
          args[i++] = str;
 
121
        }
 
122
    }
 
123
 
 
124
  /* Execute the GRUB command or function.  */
 
125
  if (grubcmd)
 
126
    {
 
127
      /* Count the amount of options the command has.  */
 
128
      parser = (struct grub_arg_option *) grubcmd->options;
 
129
      while (parser && (parser++)->doc)
 
130
        maxargs++;
 
131
      
 
132
      /* Set up the option state.  */
 
133
      state = grub_malloc (sizeof (struct grub_arg_list) * maxargs);
 
134
      grub_memset (state, 0, sizeof (struct grub_arg_list) * maxargs);
 
135
  
 
136
      /* Start the command.  */
 
137
      if (! (grubcmd->flags & GRUB_COMMAND_FLAG_NO_ARG_PARSE))
 
138
        {
 
139
          if (grub_arg_parse (grubcmd, argcount, args, state, &parsed_arglist, &numargs))
 
140
            ret = (grubcmd->func) (state, numargs, parsed_arglist);
 
141
        }
 
142
      else
 
143
        ret = (grubcmd->func) (state, argcount, args);
 
144
  
 
145
      grub_free (state);
 
146
    }
 
147
  else
 
148
    ret = grub_script_function_call (func, argcount, args);
 
149
 
 
150
  /* Free arguments.  */
 
151
  for (i = 0; i < argcount; i++)
 
152
    grub_free (args[i]);
 
153
  grub_free (args);
 
154
 
 
155
  grub_sprintf (errnobuf, "%d", ret);
 
156
  grub_env_set ("?", errnobuf);
 
157
 
 
158
  return ret;
 
159
}
 
160
 
 
161
/* Execute a block of one or more commands.  */  
 
162
grub_err_t
 
163
grub_script_execute_cmdblock (struct grub_script_cmd *cmd)
 
164
{
 
165
  struct grub_script_cmdblock *cmdblock = (struct grub_script_cmdblock *) cmd;
 
166
 
 
167
  /* Loop over every command and execute it.  */
 
168
  for (cmd = cmdblock->cmdlist; cmd; cmd = cmd->next)
 
169
    grub_script_execute_cmd (cmd);
 
170
 
 
171
  return 0;
 
172
}
 
173
 
 
174
/* Execute an if statement.  */
 
175
grub_err_t
 
176
grub_script_execute_cmdif (struct grub_script_cmd *cmd)
 
177
{
 
178
  struct grub_script_cmdif *cmdif = (struct grub_script_cmdif *) cmd;
 
179
  char *bool;
 
180
 
 
181
  /* Check if the commands results in a true or a false.  The value is
 
182
     read from the env variable `RESULT'.  */
 
183
  grub_script_execute_cmd (cmdif->bool);
 
184
  bool = grub_env_get ("?");
 
185
 
 
186
  /* Execute the `if' or the `else' part depending on the value of
 
187
     `RESULT'.  */
 
188
  if (bool && ! grub_strcmp (bool, "0"))
 
189
    return grub_script_execute_cmd (cmdif->true);
 
190
  else
 
191
    return grub_script_execute_cmd (cmdif->false);
 
192
}
 
193
 
 
194
 
 
195
 
 
196
/* Execute any GRUB pre-parsed command or script.  */
 
197
grub_err_t
 
198
grub_script_execute (struct grub_script *script)
 
199
{
 
200
  if (script == 0)
 
201
    return 0;
 
202
 
 
203
  return grub_script_execute_cmd (script->cmd);
 
204
}