function.c 2.74 KB
Newer Older
1 2
/*
 *  GRUB  --  GRand Unified Bootloader
3
 *  Copyright (C) 2005,2007,2009,2010  Free Software Foundation, Inc.
4
 *
5
 *  GRUB is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation, either version 3 of the License, or
8 9
 *  (at your option) any later version.
 *
10
 *  GRUB is distributed in the hope that it will be useful,
11 12 13 14 15
 *  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
16
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
17 18 19
 */

#include <grub/misc.h>
20
#include <grub/script_sh.h>
21 22
#include <grub/parser.h>
#include <grub/mm.h>
23
#include <grub/charset.h>
24

25
grub_script_function_t grub_script_function_list;
26 27

grub_script_function_t
28
grub_script_function_create (struct grub_script_arg *functionname_arg,
29
			     struct grub_script *cmd)
30 31 32
{
  grub_script_function_t func;
  grub_script_function_t *p;
33

34 35 36 37
  func = (grub_script_function_t) grub_malloc (sizeof (*func));
  if (! func)
    return 0;

38
  func->name = grub_strdup (functionname_arg->str);
39 40 41 42 43
  if (! func->name)
    {
      grub_free (func);
      return 0;
    }
44

45 46 47 48 49 50
  func->func = cmd;

  /* Keep the list sorted for simplicity.  */
  p = &grub_script_function_list;
  while (*p)
    {
51
      if (grub_strcmp ((*p)->name, func->name) >= 0)
52 53 54 55 56 57
	break;

      p = &((*p)->next);
    }

  /* If the function already exists, overwrite the old function.  */
58
  if (*p && grub_strcmp ((*p)->name, func->name) == 0)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    {
      grub_script_function_t q;

      q = *p;
      grub_script_free (q->func);
      q->func = cmd;
      grub_free (func);
      func = q;
    }
  else
    {
      func->next = *p;
      *p = func;
    }

  return func;
}

void
grub_script_function_remove (const char *name)
{
  grub_script_function_t *p, q;

  for (p = &grub_script_function_list, q = *p; q; p = &(q->next), q = q->next)
    if (grub_strcmp (name, q->name) == 0)
      {
        *p = q->next;
	grub_free (q->name);
	grub_script_free (q->func);
        grub_free (q);
        break;
      }
}

grub_script_function_t
grub_script_function_find (char *functionname)
{
  grub_script_function_t func;

  for (func = grub_script_function_list; func; func = func->next)
    if (grub_strcmp (functionname, func->name) == 0)
      break;

  if (! func)
103 104 105 106 107 108
    {
      char tmp[21];
      grub_strncpy (tmp, functionname, 20);
      tmp[20] = 0;
      /* Avoid truncating inside UTF-8 character.  */
      tmp[grub_getend (tmp, tmp + grub_strlen (tmp))] = 0;
109
      grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("can't find command `%s'"), tmp);
110
    }
111 112 113

  return func;
}