~ubuntu-branches/ubuntu/lucid/grub2/lucid

« back to all changes in this revision

Viewing changes to kern/parser.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060105152040-1ab076d4n3y2o5yf
Tags: 1.92-1
* New upstream release.
  - Add support for GPT partition table format.
  - Add a new command "play" to play an audio file on PC.
  - Add support for Linux/ADFS partition table format.
  - Add support for BASH-like scripting.
  - Add support for Apple HFS+ filesystems.
* 01_fix_grub-install.patch: Added. Fix grub-install to use
  /bin/grub-mkimage instead of /sbin/grub-mkimage. Closes: #338824
* Do not use CDBS tarball mode anymore. Closes: #344272  

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* parser.c - the part of the parser that can return partial tokens */
 
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/parser.h>
 
22
#include <grub/env.h>
 
23
#include <grub/misc.h>
 
24
#include <grub/mm.h>
 
25
 
 
26
 
 
27
/* All the possible state transitions on the command line.  If a
 
28
   transition can not be found, it is assumed that there is no
 
29
   transition and keep_value is assumed to be 1.  */
 
30
static struct grub_parser_state_transition state_transitions[] =
 
31
{
 
32
  { GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_QUOTE, '\'', 0},
 
33
  { GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_DQUOTE, '\"', 0},
 
34
  { GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_VAR, '$', 0},
 
35
  { GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_ESC, '\\', 0},
 
36
 
 
37
  { GRUB_PARSER_STATE_ESC, GRUB_PARSER_STATE_TEXT, 0, 1},
 
38
 
 
39
  { GRUB_PARSER_STATE_QUOTE, GRUB_PARSER_STATE_TEXT, '\'', 0},
 
40
 
 
41
  { GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_TEXT, '\"', 0},
 
42
  { GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_QVAR, '$', 0},
 
43
 
 
44
  { GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME2, '{', 0},
 
45
  { GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME, 0, 1},
 
46
  { GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1},
 
47
  { GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},
 
48
 
 
49
  { GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME2, '{', 0},
 
50
  { GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME, 0, 1},
 
51
  { GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, ' ', 1},
 
52
  { GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_TEXT, '\"', 0},
 
53
  { GRUB_PARSER_STATE_QVARNAME2, GRUB_PARSER_STATE_DQUOTE, '}', 0},
 
54
 
 
55
  { 0, 0, 0, 0}
 
56
};
 
57
 
 
58
 
 
59
/* Determines the state following STATE, determined by C.  */
 
60
grub_parser_state_t
 
61
grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
 
62
{
 
63
  struct grub_parser_state_transition *transition;
 
64
  struct grub_parser_state_transition *next_match = 0;
 
65
  struct grub_parser_state_transition default_transition;
 
66
  int found = 0;
 
67
 
 
68
  default_transition.to_state = state;
 
69
  default_transition.keep_value = 1;
 
70
 
 
71
  /* Look for a good translation.  */
 
72
  for (transition = state_transitions; transition->from_state; transition++)
 
73
    {
 
74
      /* An exact match was found, use it.  */
 
75
      if (transition->from_state == state && transition->input == c)
 
76
        {
 
77
          found = 1;
 
78
          break;
 
79
        }
 
80
 
 
81
      /* A less perfect match was found, use this one if no exact
 
82
         match can be found.  */
 
83
      if (transition->from_state == state && transition->input == 0)
 
84
        next_match = transition;
 
85
    }
 
86
 
 
87
  if (! found)
 
88
    {
 
89
      if (next_match)
 
90
        transition = next_match;
 
91
      else
 
92
        transition = &default_transition;
 
93
    }
 
94
 
 
95
  if (transition->keep_value)
 
96
    *result = c;
 
97
  else
 
98
    *result = 0;
 
99
  return transition->to_state;
 
100
}
 
101
 
 
102
 
 
103
grub_err_t
 
104
grub_parser_split_cmdline (const char *cmdline, grub_err_t (*getline) (char **),
 
105
                           int *argc, char ***argv)
 
106
{
 
107
  grub_parser_state_t state = GRUB_PARSER_STATE_TEXT;
 
108
  /* XXX: Fixed size buffer, perhaps this buffer should be dynamically
 
109
     allocated.  */
 
110
  char buffer[1024];
 
111
  char *bp = buffer;
 
112
  char *rd = (char *) cmdline;
 
113
  char varname[200];
 
114
  char *vp = varname;
 
115
  char *args;
 
116
  int i;
 
117
 
 
118
  auto int check_varstate (grub_parser_state_t s);
 
119
 
 
120
  int check_varstate (grub_parser_state_t s)
 
121
    {
 
122
      return (s == GRUB_PARSER_STATE_VARNAME
 
123
              || s == GRUB_PARSER_STATE_VARNAME2
 
124
              || s == GRUB_PARSER_STATE_QVARNAME
 
125
              || s == GRUB_PARSER_STATE_QVARNAME2);
 
126
    }
 
127
 
 
128
  auto void add_var (grub_parser_state_t newstate);
 
129
 
 
130
  void add_var (grub_parser_state_t newstate)
 
131
    {
 
132
      char *val;
 
133
 
 
134
      /* Check if a variable was being read in and the end of the name
 
135
         was reached.  */
 
136
      if (! (check_varstate (state) && !check_varstate (newstate)))
 
137
        return;
 
138
 
 
139
      *(vp++) = '\0';
 
140
      val = grub_env_get (varname);
 
141
      vp = varname;
 
142
      if (! val)
 
143
        return;
 
144
      
 
145
      /* Insert the contents of the variable in the buffer.  */
 
146
      for (; *val; val++)
 
147
        *(bp++) = *val;
 
148
    }
 
149
 
 
150
  *argc = 1;
 
151
  do
 
152
    {
 
153
      if (! *rd)
 
154
        {
 
155
          if (getline)
 
156
            getline (&rd);
 
157
          else break;
 
158
        }
 
159
 
 
160
      for (; *rd; rd++)
 
161
        {
 
162
          grub_parser_state_t newstate;
 
163
          char use;
 
164
          
 
165
          newstate = grub_parser_cmdline_state (state, *rd, &use);
 
166
 
 
167
          /* If a variable was being processed and this character does
 
168
             not describe the variable anymore, write the variable to
 
169
             the buffer.  */
 
170
          add_var (newstate);
 
171
 
 
172
          if (check_varstate (newstate))
 
173
            {
 
174
              if (use)
 
175
                *(vp++) = use;
 
176
            }
 
177
          else
 
178
            {
 
179
              if (newstate == GRUB_PARSER_STATE_TEXT
 
180
                  && state != GRUB_PARSER_STATE_ESC && use == ' ')
 
181
                {
 
182
                  /* Don't add more than one argument if multiple
 
183
                     spaces are used.  */
 
184
                  if (bp != buffer && *(bp - 1))
 
185
                    {
 
186
                      *(bp++) = '\0';
 
187
                      (*argc)++;
 
188
                    }
 
189
                }
 
190
              else if (use)
 
191
                *(bp++) = use;
 
192
            }
 
193
          state = newstate;
 
194
        }
 
195
    } while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
 
196
  *(bp++) = '\0';
 
197
 
 
198
  /* A special case for when the last character was part of a
 
199
     variable.  */
 
200
  add_var (GRUB_PARSER_STATE_TEXT);
 
201
  
 
202
 
 
203
  /* Reserve memory for the return values.  */
 
204
  args = grub_malloc (bp - buffer);
 
205
  if (! args)
 
206
    return grub_errno;
 
207
  grub_memcpy (args, buffer, bp - buffer);
 
208
  
 
209
  *argv = grub_malloc (sizeof (char *) * (*argc + 1));
 
210
  if (! *argv)
 
211
    {
 
212
      grub_free (args);
 
213
      return grub_errno;
 
214
    }
 
215
 
 
216
  /* The arguments are separated with 0's, setup argv so it points to
 
217
     the right values.  */
 
218
  bp = args;
 
219
  for (i = 0; i < *argc; i++)
 
220
    {
 
221
      (*argv)[i] = bp;
 
222
      while (*bp)
 
223
        bp++;
 
224
      bp++;
 
225
    }
 
226
 
 
227
  (*argc)--;
 
228
 
 
229
  return 0;
 
230
}