grub_err_t grub_rescue_parse_line (char *line, grub_reader_getline_t getline, void *getline_data) { char *name; int n; grub_command_t cmd; char **args; if (grub_parser_split_cmdline (line, getline, getline_data, &n, &args) || n < 0) return grub_errno; if (n == 0) return GRUB_ERR_NONE; /* In case of an assignment set the environment accordingly instead of calling a function. */ if (n == 1) { char *val = grub_strchr (args[0], '='); if (val) { val[0] = 0; grub_env_set (args[0], val + 1); val[0] = '='; goto quit; } } /* Get the command name. */ name = args[0]; /* If nothing is specified, restart. */ if (*name == '\0') goto quit; cmd = grub_command_find (name); if (cmd) { (cmd->func) (cmd, n - 1, &args[1]); } else { grub_printf_ (N_("Unknown command `%s'.\n"), name); if (grub_command_find ("help")) grub_printf ("Try `help' for usage\n"); } quit: /* Arguments are returned in single memory chunk separated by zeroes */ grub_free (args[0]); grub_free (args); return grub_errno; }
static grub_err_t grub_rescue_parse_line (char *line, grub_reader_getline_t getline, void *closure) { char *name; int n; grub_command_t cmd; char **args; if (grub_parser_split_cmdline (line, getline, closure, &n, &args) || n < 0) return grub_errno; if (n == 0) return GRUB_ERR_NONE; /* In case of an assignment set the environment accordingly instead of calling a function. */ if (n == 1 && grub_strchr (line, '=')) { char *val = grub_strchr (args[0], '='); val[0] = 0; grub_env_set (args[0], val + 1); val[0] = '='; goto quit; } /* Get the command name. */ name = args[0]; /* If nothing is specified, restart. */ if (*name == '\0') goto quit; cmd = grub_command_find (name); if (cmd) { (cmd->func) (cmd, n - 1, &args[1]); } else { grub_printf ("Unknown command `%s'\n", name); if (grub_command_find ("help")) grub_printf ("Try `help' for usage\n"); } quit: grub_free (args[0]); grub_free (args); return grub_errno; }
static grub_err_t grub_cmd_time (grub_command_t ctxt __attribute__ ((unused)), int argc, char **args) { grub_command_t cmd; grub_uint32_t start; grub_uint32_t end; if (argc == 0) return grub_error (GRUB_ERR_BAD_ARGUMENT, "command expected"); cmd = grub_command_find (args[0]); if (!cmd) return grub_error (GRUB_ERR_UNKNOWN_COMMAND, "Unknown command `%s'\n", args[0]); start = grub_get_time_ms (); (cmd->func) (cmd, argc - 1, &args[1]); end = grub_get_time_ms (); grub_printf ("Elapsed time: %d.%03d seconds \n", (end - start) / 1000, (end - start) % 1000); return grub_errno; }
static grub_err_t grub_dyncmd_dispatcher (struct grub_command *cmd, int argc, char **args) { char *modname = cmd->data; grub_dl_t mod; grub_err_t ret; mod = grub_dl_load (modname); if (mod) { char *name; grub_free (modname); grub_dl_ref (mod); name = (char *) cmd->name; grub_unregister_command (cmd); cmd = grub_command_find (name); if (cmd) ret = (cmd->func) (cmd, argc, args); else ret = grub_errno; grub_free (name); } else ret = grub_errno; return ret; }
static grub_err_t execute_command (char *name, int n, char **args) { grub_command_t cmd; cmd = grub_command_find (name); if (! cmd) grub_util_error (_("can\'t find command %s"), name); return (cmd->func) (cmd, n, args); }
int print_command_help (grub_command_t cmd) { if (grub_command_find (cmd->name)) { if (! grub_strncmp (cmd->name, currarg, grub_strlen (currarg))) { if (cnt++ > 0) grub_printf ("\n\n"); grub_arg_show_help (cmd); } } return 0; }
int print_command_info (grub_command_t cmd) { if (grub_command_find (cmd->name)) { if (cmd->flags & GRUB_COMMAND_FLAG_CMDLINE) { char description[GRUB_TERM_WIDTH / 2]; int desclen = grub_strlen (cmd->summary); /* Make a string with a length of GRUB_TERM_WIDTH / 2 - 1 filled with the description followed by spaces. */ grub_memset (description, ' ', GRUB_TERM_WIDTH / 2 - 1); description[GRUB_TERM_WIDTH / 2 - 1] = '\0'; grub_memcpy (description, cmd->summary, (desclen < GRUB_TERM_WIDTH / 2 - 1 ? desclen : GRUB_TERM_WIDTH / 2 - 1)); grub_printf ("%s%s", description, (cnt++) % 2 ? "\n" : " "); } } return 0; }
/* Read the file command.lst for auto-loading. */ void read_command_list (const char *prefix) { if (prefix) { char *filename; filename = grub_xasprintf ("%s/command.lst", prefix); if (filename) { grub_file_t file; file = grub_file_open (filename); if (file) { char *buf = NULL; grub_command_t ptr, last = 0, next; /* Override previous commands.lst. */ for (ptr = grub_command_list; ptr; ptr = next) { next = ptr->next; if (ptr->func == grub_dyncmd_dispatcher) { if (last) last->next = ptr->next; else grub_command_list = ptr->next; grub_free (ptr); } else last = ptr; } for (;; grub_free (buf)) { char *p, *name, *modname; grub_command_t cmd; int prio = 0; buf = grub_file_getline (file); if (! buf) break; name = buf; if (*name == '*') { name++; prio++; } if (! grub_isgraph (name[0])) continue; p = grub_strchr (name, ':'); if (! p) continue; *p = '\0'; while (*++p == ' ') ; if (! grub_isgraph (*p)) continue; if (grub_dl_get (p)) continue; name = grub_strdup (name); if (! name) continue; modname = grub_strdup (p); if (! modname) { grub_free (name); continue; } cmd = grub_register_command_prio (name, grub_dyncmd_dispatcher, 0, N_("not loaded"), prio); if (! cmd) { grub_free (name); grub_free (modname); continue; } cmd->flags |= GRUB_COMMAND_FLAG_DYNCMD; cmd->data = modname; /* Update the active flag. */ grub_command_find (name); } grub_file_close (file); } grub_free (filename); } } /* Ignore errors. */ grub_errno = GRUB_ERR_NONE; }
/* Execute a single command line. */ grub_err_t grub_script_execute_cmdline (struct grub_script_cmd *cmd) { struct grub_script_cmdline *cmdline = (struct grub_script_cmdline *) cmd; struct grub_script_arglist *arglist; char **args = 0; int i = 0; grub_command_t grubcmd; grub_err_t ret = 0; int argcount = 0; grub_script_function_t func = 0; char errnobuf[18]; char *cmdname; /* Lookup the command. */ cmdname = grub_script_execute_argument_to_string (cmdline->arglist->arg); grubcmd = grub_command_find (cmdname); if (! grubcmd) { grub_errno = GRUB_ERR_NONE; /* It's not a GRUB command, try all functions. */ func = grub_script_function_find (cmdname); if (! func) { /* As a last resort, try if it is an assignment. */ char *assign = grub_strdup (cmdname); char *eq = grub_strchr (assign, '='); if (eq) { /* This was set because the command was not found. */ grub_errno = GRUB_ERR_NONE; /* Create two strings and set the variable. */ *eq = '\0'; eq++; grub_env_set (assign, eq); } grub_free (assign); grub_snprintf (errnobuf, sizeof (errnobuf), "%d", grub_errno); grub_env_set ("?", errnobuf); grub_print_error (); return 0; } } grub_free (cmdname); if (cmdline->arglist->next) { argcount = cmdline->arglist->argcount - 1; /* Create argv from the arguments. */ args = grub_malloc (sizeof (char *) * argcount); for (arglist = cmdline->arglist->next; arglist; arglist = arglist->next) { char *str; str = grub_script_execute_argument_to_string (arglist->arg); args[i++] = str; } } /* Execute the GRUB command or function. */ if (grubcmd) ret = (grubcmd->func) (grubcmd, argcount, args); else ret = grub_script_function_call (func, argcount, args); /* Free arguments. */ for (i = 0; i < argcount; i++) grub_free (args[i]); grub_free (args); if (grub_errno == GRUB_ERR_TEST_FAILURE) grub_errno = GRUB_ERR_NONE; grub_print_error (); grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret); grub_env_set ("?", errnobuf); return ret; }