~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to lib/arg.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* arg.c - argument parser */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2003,2004,2005,2007,2008  Free Software Foundation, Inc.
5
 
 *
6
 
 *  GRUB 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 3 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
#include <grub/misc.h>
21
 
#include <grub/mm.h>
22
 
#include <grub/err.h>
23
 
#include <grub/term.h>
24
 
#include <grub/extcmd.h>
25
 
#include <grub/i18n.h>
26
 
 
27
 
/* Built-in parser for default options.  */
28
 
#define SHORT_ARG_HELP  -100
29
 
#define SHORT_ARG_USAGE -101
30
 
 
31
 
static const struct grub_arg_option help_options[] =
32
 
  {
33
 
    {"help", SHORT_ARG_HELP, 0,
34
 
     N_("Display this help and exit."), 0, ARG_TYPE_NONE},
35
 
    {"usage", SHORT_ARG_USAGE, 0,
36
 
     N_("Display the usage of this command and exit."), 0, ARG_TYPE_NONE},
37
 
    {0, 0, 0, 0, 0, 0}
38
 
  };
39
 
 
40
 
static struct grub_arg_option *
41
 
find_short (const struct grub_arg_option *options, char c)
42
 
{
43
 
  struct grub_arg_option *found = 0;
44
 
  auto struct grub_arg_option *fnd_short (const struct grub_arg_option *opt);
45
 
 
46
 
  struct grub_arg_option *fnd_short (const struct grub_arg_option *opt)
47
 
    {
48
 
      while (opt->doc)
49
 
        {
50
 
          if (opt->shortarg == c)
51
 
            return (struct grub_arg_option *) opt;
52
 
          opt++;
53
 
        }
54
 
      return 0;
55
 
    }
56
 
 
57
 
  if (options)
58
 
    found = fnd_short (options);
59
 
 
60
 
  if (! found)
61
 
    {
62
 
      switch (c)
63
 
        {
64
 
        case 'h':
65
 
          found = (struct grub_arg_option *) help_options;
66
 
          break;
67
 
 
68
 
        case 'u':
69
 
          found = (struct grub_arg_option *) (help_options + 1);
70
 
          break;
71
 
 
72
 
        default:
73
 
          break;
74
 
        }
75
 
    }
76
 
 
77
 
  return found;
78
 
}
79
 
 
80
 
static struct grub_arg_option *
81
 
find_long (const struct grub_arg_option *options, const char *s, int len)
82
 
{
83
 
  struct grub_arg_option *found = 0;
84
 
  auto struct grub_arg_option *fnd_long (const struct grub_arg_option *opt);
85
 
 
86
 
  struct grub_arg_option *fnd_long (const struct grub_arg_option *opt)
87
 
    {
88
 
      while (opt->doc)
89
 
        {
90
 
          if (opt->longarg && ! grub_strncmp (opt->longarg, s, len) &&
91
 
              opt->longarg[len] == '\0')
92
 
            return (struct grub_arg_option *) opt;
93
 
          opt++;
94
 
        }
95
 
      return 0;
96
 
    }
97
 
 
98
 
  if (options)
99
 
    found = fnd_long (options);
100
 
 
101
 
  if (! found)
102
 
    found = fnd_long (help_options);
103
 
 
104
 
  return found;
105
 
}
106
 
 
107
 
static void
108
 
show_usage (grub_extcmd_t cmd)
109
 
{
110
 
  grub_printf ("%s %s %s\n", _("Usage:"), cmd->cmd->name, _(cmd->cmd->summary));
111
 
}
112
 
 
113
 
void
114
 
grub_arg_show_help (grub_extcmd_t cmd)
115
 
{
116
 
  auto void showargs (const struct grub_arg_option *opt);
117
 
  int h_is_used = 0;
118
 
  int u_is_used = 0;
119
 
 
120
 
  auto void showargs (const struct grub_arg_option *opt)
121
 
    {
122
 
      for (; opt->doc; opt++)
123
 
        {
124
 
          int spacing = 20;
125
 
 
126
 
          if (opt->shortarg && grub_isgraph (opt->shortarg))
127
 
            grub_printf ("-%c%c ", opt->shortarg, opt->longarg ? ',':' ');
128
 
          else if (opt->shortarg == SHORT_ARG_HELP && ! h_is_used)
129
 
            grub_printf ("-h, ");
130
 
          else if (opt->shortarg == SHORT_ARG_USAGE && ! u_is_used)
131
 
            grub_printf ("-u, ");
132
 
          else
133
 
            grub_printf ("    ");
134
 
 
135
 
          if (opt->longarg)
136
 
            {
137
 
              grub_printf ("--%s", opt->longarg);
138
 
              spacing -= grub_strlen (opt->longarg) + 2;
139
 
 
140
 
              if (opt->arg)
141
 
                {
142
 
                  grub_printf ("=%s", opt->arg);
143
 
                  spacing -= grub_strlen (opt->arg) + 1;
144
 
                }
145
 
            }
146
 
 
147
 
          while (spacing--)
148
 
            grub_xputs (" ");
149
 
 
150
 
          grub_printf ("%s\n", _(opt->doc));
151
 
 
152
 
          switch (opt->shortarg)
153
 
            {
154
 
            case 'h':
155
 
              h_is_used = 1;
156
 
              break;
157
 
 
158
 
            case 'u':
159
 
              u_is_used = 1;
160
 
              break;
161
 
 
162
 
            default:
163
 
              break;
164
 
            }
165
 
        }
166
 
    }
167
 
 
168
 
  show_usage (cmd);
169
 
  grub_printf ("%s\n\n", _(cmd->cmd->description));
170
 
  if (cmd->options)
171
 
    showargs (cmd->options);
172
 
  showargs (help_options);
173
 
#if 0
174
 
  grub_printf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
175
 
#endif
176
 
}
177
 
 
178
 
 
179
 
static int
180
 
parse_option (grub_extcmd_t cmd, int key, char *arg, struct grub_arg_list *usr)
181
 
{
182
 
  switch (key)
183
 
    {
184
 
    case SHORT_ARG_HELP:
185
 
      grub_arg_show_help (cmd);
186
 
      return -1;
187
 
 
188
 
    case SHORT_ARG_USAGE:
189
 
      show_usage (cmd);
190
 
      return -1;
191
 
 
192
 
    default:
193
 
      {
194
 
        int found = -1;
195
 
        int i = 0;
196
 
        const struct grub_arg_option *opt = cmd->options;
197
 
 
198
 
        while (opt->doc)
199
 
          {
200
 
            if (opt->shortarg && key == opt->shortarg)
201
 
              {
202
 
                found = i;
203
 
                break;
204
 
              }
205
 
            opt++;
206
 
            i++;
207
 
          }
208
 
 
209
 
        if (found == -1)
210
 
          return -1;
211
 
 
212
 
        usr[found].set = 1;
213
 
        usr[found].arg = arg;
214
 
      }
215
 
    }
216
 
 
217
 
  return 0;
218
 
}
219
 
 
220
 
int
221
 
grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,
222
 
                struct grub_arg_list *usr, char ***args, int *argnum)
223
 
{
224
 
  int curarg;
225
 
  int arglen;
226
 
  int complete = 0;
227
 
  char **argl = 0;
228
 
  int num = 0;
229
 
  auto grub_err_t add_arg (char *s);
230
 
 
231
 
  grub_err_t add_arg (char *s)
232
 
    {
233
 
      argl = grub_realloc (argl, (++num) * sizeof (char *));
234
 
      if (! argl)
235
 
        return grub_errno;
236
 
      argl[num - 1] = s;
237
 
      return 0;
238
 
    }
239
 
 
240
 
 
241
 
  for (curarg = 0; curarg < argc; curarg++)
242
 
    {
243
 
      char *arg = argv[curarg];
244
 
      struct grub_arg_option *opt;
245
 
      char *option = 0;
246
 
 
247
 
      /* No option is used.  */
248
 
      if (arg[0] != '-' || grub_strlen (arg) == 1)
249
 
        {
250
 
          if (add_arg (arg) != 0)
251
 
            goto fail;
252
 
 
253
 
          continue;
254
 
        }
255
 
 
256
 
      /* One or more short options.  */
257
 
      if (arg[1] != '-')
258
 
        {
259
 
          char *curshort = arg + 1;
260
 
 
261
 
          while (1)
262
 
            {
263
 
              opt = find_short (cmd->options, *curshort);
264
 
              if (! opt)
265
 
                {
266
 
                  grub_error (GRUB_ERR_BAD_ARGUMENT,
267
 
                              "unknown argument `-%c'", *curshort);
268
 
                  goto fail;
269
 
                }
270
 
 
271
 
              curshort++;
272
 
 
273
 
              /* Parse all arguments here except the last one because
274
 
                 it can have an argument value.  */
275
 
              if (*curshort)
276
 
                {
277
 
                  if (parse_option (cmd, opt->shortarg, 0, usr) || grub_errno)
278
 
                    goto fail;
279
 
                }
280
 
              else
281
 
                {
282
 
                  if (opt->type != ARG_TYPE_NONE)
283
 
                    {
284
 
                      if (curarg + 1 < argc)
285
 
                        {
286
 
                          char *nextarg = argv[curarg + 1];
287
 
                          if (!(opt->flags & GRUB_ARG_OPTION_OPTIONAL)
288
 
                              || (grub_strlen (nextarg) < 2 || nextarg[0] != '-'))
289
 
                            option = argv[++curarg];
290
 
                        }
291
 
                    }
292
 
                  break;
293
 
                }
294
 
            }
295
 
 
296
 
        }
297
 
      else /* The argument starts with "--".  */
298
 
        {
299
 
          /* If the argument "--" is used just pass the other
300
 
             arguments.  */
301
 
          if (grub_strlen (arg) == 2)
302
 
            {
303
 
              for (curarg++; curarg < argc; curarg++)
304
 
                if (add_arg (argv[curarg]) != 0)
305
 
                  goto fail;
306
 
              break;
307
 
            }
308
 
 
309
 
          option = grub_strchr (arg, '=');
310
 
          if (option) {
311
 
            arglen = option - arg - 2;
312
 
            option++;
313
 
          } else
314
 
            arglen = grub_strlen (arg) - 2;
315
 
 
316
 
          opt = find_long (cmd->options, arg + 2, arglen);
317
 
          if (! opt)
318
 
            {
319
 
              grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown argument `%s'", arg);
320
 
              goto fail;
321
 
            }
322
 
        }
323
 
 
324
 
      if (! (opt->type == ARG_TYPE_NONE
325
 
             || (! option && (opt->flags & GRUB_ARG_OPTION_OPTIONAL))))
326
 
        {
327
 
          if (! option)
328
 
            {
329
 
              grub_error (GRUB_ERR_BAD_ARGUMENT,
330
 
                          "missing mandatory option for `%s'", opt->longarg);
331
 
              goto fail;
332
 
            }
333
 
 
334
 
          switch (opt->type)
335
 
            {
336
 
            case ARG_TYPE_NONE:
337
 
              /* This will never happen.  */
338
 
              break;
339
 
 
340
 
            case ARG_TYPE_STRING:
341
 
                  /* No need to do anything.  */
342
 
              break;
343
 
 
344
 
            case ARG_TYPE_INT:
345
 
              {
346
 
                char *tail;
347
 
 
348
 
                grub_strtoull (option, &tail, 0);
349
 
                if (tail == 0 || tail == option || *tail != '\0' || grub_errno)
350
 
                  {
351
 
                    grub_error (GRUB_ERR_BAD_ARGUMENT,
352
 
                                "the argument `%s' requires an integer",
353
 
                                arg);
354
 
 
355
 
                    goto fail;
356
 
                  }
357
 
                break;
358
 
              }
359
 
 
360
 
            case ARG_TYPE_DEVICE:
361
 
            case ARG_TYPE_DIR:
362
 
            case ARG_TYPE_FILE:
363
 
            case ARG_TYPE_PATHNAME:
364
 
              /* XXX: Not implemented.  */
365
 
              break;
366
 
            }
367
 
          if (parse_option (cmd, opt->shortarg, option, usr) || grub_errno)
368
 
            goto fail;
369
 
        }
370
 
      else
371
 
        {
372
 
          if (option)
373
 
            {
374
 
              grub_error (GRUB_ERR_BAD_ARGUMENT,
375
 
                          "a value was assigned to the argument `%s' while it "
376
 
                          "doesn't require an argument", arg);
377
 
              goto fail;
378
 
            }
379
 
 
380
 
          if (parse_option (cmd, opt->shortarg, 0, usr) || grub_errno)
381
 
            goto fail;
382
 
        }
383
 
    }
384
 
 
385
 
  complete = 1;
386
 
 
387
 
  *args = argl;
388
 
  *argnum = num;
389
 
 
390
 
 fail:
391
 
  return complete;
392
 
}