arg.c 9.48 KB
Newer Older
1 2
/* arg.c - argument parser */
/*
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 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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 21 22 23
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/term.h>
24
#include <grub/extcmd.h>
25
#include <grub/i18n.h>
26 27

/* Built-in parser for default options.  */
28
static const struct grub_arg_option help_options[] =
29
  {
30
    {"help", 0, 0,
31
     N_("Display this help and exit."), 0, ARG_TYPE_NONE},
32
    {"usage", 0, 0,
33
     N_("Display the usage of this command and exit."), 0, ARG_TYPE_NONE},
34 35 36
    {0, 0, 0, 0, 0, 0}
  };

37
/* Helper for find_short.  */
38
static const struct grub_arg_option *
39
fnd_short (const struct grub_arg_option *opt, char c)
40
{
41
  while (opt->doc)
42
    {
43
      if (opt->shortarg == c)
44
	return opt;
45
      opt++;
46
    }
47 48 49
  return 0;
}

50
static const struct grub_arg_option *
51 52
find_short (const struct grub_arg_option *options, char c)
{
53
  const struct grub_arg_option *found = 0;
54 55

  if (options)
56
    found = fnd_short (options, c);
57

58
  if (! found)
59 60 61 62
    {
      switch (c)
	{
	case 'h':
63
	  found = help_options;
64 65 66
	  break;

	case 'u':
67
	  found = (help_options + 1);
68 69 70 71 72 73
	  break;

	default:
	  break;
	}
    }
74

75 76 77
  return found;
}

78
/* Helper for find_long.  */
79
static const struct grub_arg_option *
80
fnd_long (const struct grub_arg_option *opt, const char *s, int len)
81
{
82
  while (opt->doc)
83
    {
84 85
      if (opt->longarg && ! grub_strncmp (opt->longarg, s, len) &&
	  opt->longarg[len] == '\0')
86
	return opt;
87
      opt++;
88
    }
89 90 91
  return 0;
}

92
static const struct grub_arg_option *
93 94
find_long (const struct grub_arg_option *options, const char *s, int len)
{
95
  const struct grub_arg_option *found = 0;
96 97

  if (options)
98
    found = fnd_long (options, s, len);
99

100
  if (! found)
101
    found = fnd_long (help_options, s, len);
102

103 104 105 106
  return found;
}

static void
107
show_usage (grub_extcmd_t cmd)
108
{
109
  grub_printf ("%s %s %s\n", _("Usage:"), cmd->cmd->name, _(cmd->cmd->summary));
110 111
}

112 113 114
static void
showargs (const struct grub_arg_option *opt,
	  int h_is_used, int u_is_used)
115
{
116
  for (; opt->doc; opt++)
117
    {
118 119 120 121 122 123 124 125 126 127 128 129
      int spacing = 20;

      if (opt->shortarg && grub_isgraph (opt->shortarg))
	grub_printf ("-%c%c ", opt->shortarg, opt->longarg ? ',':' ');
      else if (opt == help_options && ! h_is_used)
	grub_printf ("-h, ");
      else if (opt == help_options + 1 && ! u_is_used)
	grub_printf ("-u, ");
      else
	grub_printf ("    ");

      if (opt->longarg)
130
	{
131 132 133 134
	  grub_printf ("--%s", opt->longarg);
	  spacing -= grub_strlen (opt->longarg) + 2;

	  if (opt->arg)
135
	    {
136 137
	      grub_printf ("=%s", opt->arg);
	      spacing -= grub_strlen (opt->arg) + 1;
138
	    }
139
	}
140

141
      if (spacing <= 0)
142
	spacing = 3;
143

144 145
      while (spacing--)
	grub_xputs (" ");
146

147
      grub_printf ("%s\n", _(opt->doc));
148
    }
149 150 151 152 153 154 155 156
}

void
grub_arg_show_help (grub_extcmd_t cmd)
{
  int h_is_used = 0;
  int u_is_used = 0;
  const struct grub_arg_option *opt;
157 158

  show_usage (cmd);
159
  grub_printf ("%s\n\n", _(cmd->cmd->description));
160

161
  for (opt = cmd->options; opt && opt->doc; opt++)
162 163 164 165 166 167 168 169 170 171 172
    switch (opt->shortarg)
      {
      case 'h':
	h_is_used = 1;
	break;

      case 'u':
	u_is_used = 1;
	break;
      }

173
  if (cmd->options)
174 175
    showargs (cmd->options, h_is_used, u_is_used);
  showargs (help_options, h_is_used, u_is_used);
176
#if 0
177
  grub_printf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
178
#endif
179 180 181 182
}


static int
183 184
parse_option (grub_extcmd_t cmd, const struct grub_arg_option *opt,
	      char *arg, struct grub_arg_list *usr)
185
{
186
  if (opt == help_options)
187
    {
188
      grub_arg_show_help (cmd);
189
      return -1;
190
    }
191

192 193
  if (opt == help_options + 1)
    {
194 195
      show_usage (cmd);
      return -1;
196 197 198
    }
  {
    int found = opt - cmd->options;
199

200
    if (opt->flags & GRUB_ARG_OPTION_REPEATABLE)
201
      {
202 203
	usr[found].args[usr[found].set++] = arg;
	usr[found].args[usr[found].set] = NULL;
204
      }
205 206 207 208 209 210
    else
      {
	usr[found].set = 1;
	usr[found].arg = arg;
      }
  }
211

212 213 214
  return 0;
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
static inline grub_err_t
add_arg (char ***argl, int *num, char *s)
{
  char **p = *argl;
  *argl = grub_realloc (*argl, (++(*num) + 1) * sizeof (char *));
  if (! *argl)
    {
      grub_free (p);
      return grub_errno;
    }
  (*argl)[(*num) - 1] = s;
  (*argl)[(*num)] = NULL;
  return 0;
}


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 238 239 240 241 242
  char **argl = 0;
  int num = 0;

  for (curarg = 0; curarg < argc; curarg++)
    {
      char *arg = argv[curarg];
243
      const struct grub_arg_option *opt;
244 245 246
      char *option = 0;

      /* No option is used.  */
247
      if ((num && (cmd->cmd->flags & GRUB_COMMAND_OPTIONS_AT_START))
248
	  || arg[0] != '-' || grub_strlen (arg) == 1)
249
	{
250
	  if (add_arg (&argl, &num, arg) != 0)
251
	    goto fail;
252

253 254 255 256 257 258
	  continue;
	}

      /* One or more short options.  */
      if (arg[1] != '-')
	{
259 260 261 262 263 264 265 266 267 268
	  char *curshort;

	  if (cmd->cmd->flags & GRUB_COMMAND_ACCEPT_DASH)
	    {
	      for (curshort = arg + 1; *curshort; curshort++)
		if (!find_short (cmd->options, *curshort))
		  break;
	    
	      if (*curshort)
		{
269
		  if (add_arg (&argl, &num, arg) != 0)
270 271 272 273 274 275
		    goto fail;
		  continue;
		}
	    }

	  curshort = arg + 1;
276 277 278 279

	  while (1)
	    {
	      opt = find_short (cmd->options, *curshort);
280

281
	      if (! opt)
282
		{
283
		  char tmp[3] = { '-', *curshort, 0 };
284
		  grub_error (GRUB_ERR_BAD_ARGUMENT,
285
			      N_("unknown argument `%s'"), tmp);
286 287
		  goto fail;
		}
288

289 290 291 292 293 294
	      curshort++;

	      /* Parse all arguments here except the last one because
		 it can have an argument value.  */
	      if (*curshort)
		{
295
		  if (parse_option (cmd, opt, 0, usr) || grub_errno)
296 297 298 299 300 301 302 303 304
		    goto fail;
		}
	      else
		{
		  if (opt->type != ARG_TYPE_NONE)
		    {
		      if (curarg + 1 < argc)
			{
			  char *nextarg = argv[curarg + 1];
305
			  if (!(opt->flags & GRUB_ARG_OPTION_OPTIONAL)
306
			      || (grub_strlen (nextarg) < 2 || nextarg[0] != '-'))
307 308 309 310 311 312
			    option = argv[++curarg];
			}
		    }
		  break;
		}
	    }
313

314 315 316 317 318
	}
      else /* The argument starts with "--".  */
	{
	  /* If the argument "--" is used just pass the other
	     arguments.  */
319
	  if (grub_strlen (arg) == 2)
320 321
	    {
	      for (curarg++; curarg < argc; curarg++)
322
		if (add_arg (&argl, &num, argv[curarg]) != 0)
323 324 325 326
		  goto fail;
	      break;
	    }

327
	  option = grub_strchr (arg, '=');
328 329 330 331 332 333
	  if (option)
	    {
	      arglen = option - arg - 2;
	      option++;
	    }
	  else
334
	    arglen = grub_strlen (arg) - 2;
335

336
	  opt = find_long (cmd->options, arg + 2, arglen);
337

338
	  if (!option && argv[curarg + 1] && argv[curarg + 1][0] != '-'
339
	      && opt && opt->type != ARG_TYPE_NONE)
340 341
	    option = argv[++curarg];

342 343
	  if (!opt && (cmd->cmd->flags & GRUB_COMMAND_ACCEPT_DASH))
	    {
344
	      if (add_arg (&argl, &num, arg) != 0)
345 346 347 348
		goto fail;
	      continue;
	    }

349
	  if (! opt)
350
	    {
351
	      grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unknown argument `%s'"), arg);
352 353 354 355
	      goto fail;
	    }
	}

356
      if (! (opt->type == ARG_TYPE_NONE
357
	     || (! option && (opt->flags & GRUB_ARG_OPTION_OPTIONAL))))
358
	{
359
	  if (! option)
360
	    {
361
	      grub_error (GRUB_ERR_BAD_ARGUMENT,
362
			  N_("missing mandatory option for `%s'"), opt->longarg);
363 364
	      goto fail;
	    }
365

366 367 368 369 370
	  switch (opt->type)
	    {
	    case ARG_TYPE_NONE:
	      /* This will never happen.  */
	      break;
371

372 373 374
	    case ARG_TYPE_STRING:
		  /* No need to do anything.  */
	      break;
375

376 377 378
	    case ARG_TYPE_INT:
	      {
		char *tail;
379

380
		grub_strtoull (option, &tail, 0);
381
		if (tail == 0 || tail == option || *tail != '\0' || grub_errno)
382
		  {
383
		    grub_error (GRUB_ERR_BAD_ARGUMENT,
384
				N_("the argument `%s' requires an integer"),
385 386 387 388 389 390
				arg);

		    goto fail;
		  }
		break;
	      }
391

392 393 394 395 396 397 398
	    case ARG_TYPE_DEVICE:
	    case ARG_TYPE_DIR:
	    case ARG_TYPE_FILE:
	    case ARG_TYPE_PATHNAME:
	      /* XXX: Not implemented.  */
	      break;
	    }
399
	  if (parse_option (cmd, opt, option, usr) || grub_errno)
400 401 402 403 404 405
	    goto fail;
	}
      else
	{
	  if (option)
	    {
406
	      grub_error (GRUB_ERR_BAD_ARGUMENT,
407 408
			  N_("a value was assigned to the argument `%s' while it "
			     "doesn't require an argument"), arg);
409 410 411
	      goto fail;
	    }

412
	  if (parse_option (cmd, opt, 0, usr) || grub_errno)
413 414
	    goto fail;
	}
415
    }
416 417 418

  *args = argl;
  *argnum = num;
419
  return 1;
420 421

 fail:
422
  return 0;
423
}
424 425 426 427 428 429 430

struct grub_arg_list*
grub_arg_list_alloc(grub_extcmd_t extcmd, int argc,
		    char **argv __attribute__((unused)))
{
  int i;
  char **args;
431
  grub_size_t argcnt;
432 433 434 435 436 437 438 439 440 441 442
  struct grub_arg_list *list;
  const struct grub_arg_option *options;

  options = extcmd->options;
  if (! options)
    return 0;

  argcnt = 0;
  for (i = 0; options[i].doc; i++)
    {
      if (options[i].flags & GRUB_ARG_OPTION_REPEATABLE)
443
	argcnt += ((grub_size_t) argc + 1) / 2 + 1; /* max possible for any option */
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    }

  list = grub_zalloc (sizeof (*list) * i + sizeof (char*) * argcnt);
  if (! list)
    return 0;

  args = (char**) (list + i);
  for (i = 0; options[i].doc; i++)
    {
      list[i].set = 0;
      list[i].arg = 0;

      if (options[i].flags & GRUB_ARG_OPTION_REPEATABLE)
	{
	  list[i].args = args;
459
	  args += (grub_size_t) argc / 2 + 1;
460 461 462 463
	}
    }
  return list;
}