~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, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

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
 
          const char *doc = _(opt->doc);
148
 
          for (;;)
149
 
            {
150
 
              while (spacing-- > 0)
151
 
                grub_putchar (' ');
152
 
 
153
 
              while (*doc && *doc != '\n')
154
 
                grub_putchar (*doc++);
155
 
              grub_putchar ('\n');
156
 
 
157
 
              if (! *doc)
158
 
                break;
159
 
              doc++;
160
 
              spacing = 4 + 20;
161
 
            }
162
 
 
163
 
          switch (opt->shortarg)
164
 
            {
165
 
            case 'h':
166
 
              h_is_used = 1;
167
 
              break;
168
 
 
169
 
            case 'u':
170
 
              u_is_used = 1;
171
 
              break;
172
 
 
173
 
            default:
174
 
              break;
175
 
            }
176
 
        }
177
 
    }
178
 
 
179
 
  show_usage (cmd);
180
 
  grub_printf ("%s\n\n", _(cmd->cmd->description));
181
 
  if (cmd->options)
182
 
    showargs (cmd->options);
183
 
  showargs (help_options);
184
 
#if 0
185
 
  grub_printf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
186
 
#endif
187
 
}
188
 
 
189
 
 
190
 
static int
191
 
parse_option (grub_extcmd_t cmd, int key, char *arg, struct grub_arg_list *usr)
192
 
{
193
 
  switch (key)
194
 
    {
195
 
    case SHORT_ARG_HELP:
196
 
      grub_arg_show_help (cmd);
197
 
      return -1;
198
 
 
199
 
    case SHORT_ARG_USAGE:
200
 
      show_usage (cmd);
201
 
      return -1;
202
 
 
203
 
    default:
204
 
      {
205
 
        int found = -1;
206
 
        int i = 0;
207
 
        const struct grub_arg_option *opt = cmd->options;
208
 
 
209
 
        while (opt->doc)
210
 
          {
211
 
            if (opt->shortarg && key == opt->shortarg)
212
 
              {
213
 
                found = i;
214
 
                break;
215
 
              }
216
 
            opt++;
217
 
            i++;
218
 
          }
219
 
 
220
 
        if (found == -1)
221
 
          return -1;
222
 
 
223
 
        usr[found].set = 1;
224
 
        usr[found].arg = arg;
225
 
      }
226
 
    }
227
 
 
228
 
  return 0;
229
 
}
230
 
 
231
 
int
232
 
grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,
233
 
                struct grub_arg_list *usr, char ***args, int *argnum)
234
 
{
235
 
  int curarg;
236
 
  int arglen;
237
 
  int complete = 0;
238
 
  char **argl = 0;
239
 
  int num = 0;
240
 
  auto grub_err_t add_arg (char *s);
241
 
 
242
 
  grub_err_t add_arg (char *s)
243
 
    {
244
 
      argl = grub_realloc (argl, (++num) * sizeof (char *));
245
 
      if (! argl)
246
 
        return grub_errno;
247
 
      argl[num - 1] = s;
248
 
      return 0;
249
 
    }
250
 
 
251
 
 
252
 
  for (curarg = 0; curarg < argc; curarg++)
253
 
    {
254
 
      char *arg = argv[curarg];
255
 
      struct grub_arg_option *opt;
256
 
      char *option = 0;
257
 
 
258
 
      /* No option is used.  */
259
 
      if (arg[0] != '-' || grub_strlen (arg) == 1)
260
 
        {
261
 
          if (add_arg (arg) != 0)
262
 
            goto fail;
263
 
 
264
 
          continue;
265
 
        }
266
 
 
267
 
      /* One or more short options.  */
268
 
      if (arg[1] != '-')
269
 
        {
270
 
          char *curshort = arg + 1;
271
 
 
272
 
          while (1)
273
 
            {
274
 
              opt = find_short (cmd->options, *curshort);
275
 
              if (! opt)
276
 
                {
277
 
                  grub_error (GRUB_ERR_BAD_ARGUMENT,
278
 
                              "unknown argument `-%c'", *curshort);
279
 
                  goto fail;
280
 
                }
281
 
 
282
 
              curshort++;
283
 
 
284
 
              /* Parse all arguments here except the last one because
285
 
                 it can have an argument value.  */
286
 
              if (*curshort)
287
 
                {
288
 
                  if (parse_option (cmd, opt->shortarg, 0, usr) || grub_errno)
289
 
                    goto fail;
290
 
                }
291
 
              else
292
 
                {
293
 
                  if (opt->type != ARG_TYPE_NONE)
294
 
                    {
295
 
                      if (curarg + 1 < argc)
296
 
                        {
297
 
                          char *nextarg = argv[curarg + 1];
298
 
                          if (!(opt->flags & GRUB_ARG_OPTION_OPTIONAL)
299
 
                              || (grub_strlen (nextarg) < 2 || nextarg[0] != '-'))
300
 
                            option = argv[++curarg];
301
 
                        }
302
 
                    }
303
 
                  break;
304
 
                }
305
 
            }
306
 
 
307
 
        }
308
 
      else /* The argument starts with "--".  */
309
 
        {
310
 
          /* If the argument "--" is used just pass the other
311
 
             arguments.  */
312
 
          if (grub_strlen (arg) == 2)
313
 
            {
314
 
              for (curarg++; curarg < argc; curarg++)
315
 
                if (add_arg (argv[curarg]) != 0)
316
 
                  goto fail;
317
 
              break;
318
 
            }
319
 
 
320
 
          option = grub_strchr (arg, '=');
321
 
          if (option) {
322
 
            arglen = option - arg - 2;
323
 
            option++;
324
 
          } else
325
 
            arglen = grub_strlen (arg) - 2;
326
 
 
327
 
          opt = find_long (cmd->options, arg + 2, arglen);
328
 
          if (! opt)
329
 
            {
330
 
              grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown argument `%s'", arg);
331
 
              goto fail;
332
 
            }
333
 
        }
334
 
 
335
 
      if (! (opt->type == ARG_TYPE_NONE
336
 
             || (! option && (opt->flags & GRUB_ARG_OPTION_OPTIONAL))))
337
 
        {
338
 
          if (! option)
339
 
            {
340
 
              grub_error (GRUB_ERR_BAD_ARGUMENT,
341
 
                          "missing mandatory option for `%s'", opt->longarg);
342
 
              goto fail;
343
 
            }
344
 
 
345
 
          switch (opt->type)
346
 
            {
347
 
            case ARG_TYPE_NONE:
348
 
              /* This will never happen.  */
349
 
              break;
350
 
 
351
 
            case ARG_TYPE_STRING:
352
 
                  /* No need to do anything.  */
353
 
              break;
354
 
 
355
 
            case ARG_TYPE_INT:
356
 
              {
357
 
                char *tail;
358
 
 
359
 
                grub_strtoull (option, &tail, 0);
360
 
                if (tail == 0 || tail == option || *tail != '\0' || grub_errno)
361
 
                  {
362
 
                    grub_error (GRUB_ERR_BAD_ARGUMENT,
363
 
                                "the argument `%s' requires an integer",
364
 
                                arg);
365
 
 
366
 
                    goto fail;
367
 
                  }
368
 
                break;
369
 
              }
370
 
 
371
 
            case ARG_TYPE_DEVICE:
372
 
            case ARG_TYPE_DIR:
373
 
            case ARG_TYPE_FILE:
374
 
            case ARG_TYPE_PATHNAME:
375
 
              /* XXX: Not implemented.  */
376
 
              break;
377
 
            }
378
 
          if (parse_option (cmd, opt->shortarg, option, usr) || grub_errno)
379
 
            goto fail;
380
 
        }
381
 
      else
382
 
        {
383
 
          if (option)
384
 
            {
385
 
              grub_error (GRUB_ERR_BAD_ARGUMENT,
386
 
                          "a value was assigned to the argument `%s' while it "
387
 
                          "doesn't require an argument", arg);
388
 
              goto fail;
389
 
            }
390
 
 
391
 
          if (parse_option (cmd, opt->shortarg, 0, usr) || grub_errno)
392
 
            goto fail;
393
 
        }
394
 
    }
395
 
 
396
 
  complete = 1;
397
 
 
398
 
  *args = argl;
399
 
  *argnum = num;
400
 
 
401
 
 fail:
402
 
  return complete;
403
 
}