/* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2005,2007,2009,2010 Free Software Foundation, Inc. * * VAS_EBOOT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VAS_EBOOT is distributed in the hope that it will be useful, * 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 * along with VAS_EBOOT. If not, see . */ #include #include #include #include #include VasEBoot_script_function_t VasEBoot_script_function_list; VasEBoot_script_function_t VasEBoot_script_function_create (struct VasEBoot_script_arg *functionname_arg, struct VasEBoot_script *cmd) { VasEBoot_script_function_t func; VasEBoot_script_function_t *p; func = (VasEBoot_script_function_t) VasEBoot_malloc (sizeof (*func)); if (! func) return 0; func->executing = 0; func->name = VasEBoot_strdup (functionname_arg->str); if (! func->name) { VasEBoot_free (func); return 0; } func->func = cmd; /* Keep the list sorted for simplicity. */ p = &VasEBoot_script_function_list; while (*p) { if (VasEBoot_strcmp ((*p)->name, func->name) >= 0) break; p = &((*p)->next); } /* If the function already exists, overwrite the old function. */ if (*p && VasEBoot_strcmp ((*p)->name, func->name) == 0) { VasEBoot_script_function_t q; q = *p; VasEBoot_free (func); if (q->executing > 0) { VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("attempt to redefine a function being executed")); func = NULL; } else { VasEBoot_script_free (q->func); q->func = cmd; func = q; } } else { func->next = *p; *p = func; } return func; } void VasEBoot_script_function_remove (const char *name) { VasEBoot_script_function_t *p, q; for (p = &VasEBoot_script_function_list, q = *p; q; p = &(q->next), q = q->next) if (VasEBoot_strcmp (name, q->name) == 0) { *p = q->next; VasEBoot_free (q->name); VasEBoot_script_free (q->func); VasEBoot_free (q); break; } } VasEBoot_script_function_t VasEBoot_script_function_find (char *functionname) { VasEBoot_script_function_t func; for (func = VasEBoot_script_function_list; func; func = func->next) if (VasEBoot_strcmp (functionname, func->name) == 0) break; if (! func) { char tmp[21]; VasEBoot_strncpy (tmp, functionname, 20); tmp[20] = 0; /* Avoid truncating inside UTF-8 character. */ tmp[VasEBoot_getend (tmp, tmp + VasEBoot_strlen (tmp))] = 0; VasEBoot_error (VAS_EBOOT_ERR_UNKNOWN_COMMAND, N_("can't find command `%s'"), tmp); } return func; }