📄 cli-decode.c
字号:
/* This used to say *result_list = *found->prefixlist If that was correct, need to modify the documentation at the top of this function to clarify what is supposed to be going on. */ *result_list = found; return c; } else { /* We matched! */ return c; } } else { if (result_list != NULL) *result_list = clist; return found; }}/* All this hair to move the space to the front of cmdtype */static voidundef_cmd_error (char *cmdtype, char *q){ error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".", cmdtype, q, *cmdtype ? " " : "", (int) strlen (cmdtype) - 1, cmdtype);}/* Look up the contents of *LINE as a command in the command list LIST. LIST is a chain of struct cmd_list_element's. If it is found, return the struct cmd_list_element for that command and update *LINE to point after the command name, at the first argument. If not found, call error if ALLOW_UNKNOWN is zero otherwise (or if error returns) return zero. Call error if specified command is ambiguous, unless ALLOW_UNKNOWN is negative. CMDTYPE precedes the word "command" in the error message. If INGNORE_HELP_CLASSES is nonzero, ignore any command list elements which are actually help classes rather than commands (i.e. the function field of the struct cmd_list_element is 0). */struct cmd_list_element *lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype, int allow_unknown, int ignore_help_classes){ struct cmd_list_element *last_list = 0; struct cmd_list_element *c = lookup_cmd_1 (line, list, &last_list, ignore_help_classes); /* Note: Do not remove trailing whitespace here because this would be wrong for complete_command. Jim Kingdon */ if (!c) { if (!allow_unknown) { if (!*line) error ("Lack of needed %scommand", cmdtype); else { char *p = *line, *q; while (isalnum (*p) || *p == '-') p++; q = (char *) alloca (p - *line + 1); strncpy (q, *line, p - *line); q[p - *line] = '\0'; undef_cmd_error (cmdtype, q); } } else return 0; } else if (c == (struct cmd_list_element *) -1) { /* Ambigous. Local values should be off prefixlist or called values. */ int local_allow_unknown = (last_list ? last_list->allow_unknown : allow_unknown); char *local_cmdtype = last_list ? last_list->prefixname : cmdtype; struct cmd_list_element *local_list = (last_list ? *(last_list->prefixlist) : list); if (local_allow_unknown < 0) { if (last_list) return last_list; /* Found something. */ else return 0; /* Found nothing. */ } else { /* Report as error. */ int amb_len; char ambbuf[100]; for (amb_len = 0; ((*line)[amb_len] && (*line)[amb_len] != ' ' && (*line)[amb_len] != '\t'); amb_len++) ; ambbuf[0] = 0; for (c = local_list; c; c = c->next) if (!strncmp (*line, c->name, amb_len)) { if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf) { if (strlen (ambbuf)) strcat (ambbuf, ", "); strcat (ambbuf, c->name); } else { strcat (ambbuf, ".."); break; } } error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype, *line, ambbuf); return 0; /* lint */ } } else { /* We've got something. It may still not be what the caller wants (if this command *needs* a subcommand). */ while (**line == ' ' || **line == '\t') (*line)++; if (c->prefixlist && **line && !c->allow_unknown) undef_cmd_error (c->prefixname, *line); /* Seems to be what he wants. Return it. */ return c; } return 0;}/* We are here presumably because an alias or command in *TEXT is deprecated and a warning message should be generated. This function decodes *TEXT and potentially generates a warning message as outlined below. Example for 'set endian big' which has a fictitious alias 'seb'. If alias wasn't used in *TEXT, and the command is deprecated: "warning: 'set endian big' is deprecated." If alias was used, and only the alias is deprecated: "warning: 'seb' an alias for the command 'set endian big' is deprecated." If alias was used and command is deprecated (regardless of whether the alias itself is deprecated: "warning: 'set endian big' (seb) is deprecated." After the message has been sent, clear the appropriate flags in the command and/or the alias so the user is no longer bothered. */voiddeprecated_cmd_warning (char **text){ struct cmd_list_element *alias = NULL; struct cmd_list_element *prefix_cmd = NULL; struct cmd_list_element *cmd = NULL; struct cmd_list_element *c; char *type; if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd)) /* return if text doesn't evaluate to a command */ return; if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0) || (cmd->flags & DEPRECATED_WARN_USER) ) ) /* return if nothing is deprecated */ return; printf_filtered ("Warning:"); if (alias && !(cmd->flags & CMD_DEPRECATED)) printf_filtered (" '%s', an alias for the", alias->name); printf_filtered (" command '"); if (prefix_cmd) printf_filtered ("%s", prefix_cmd->prefixname); printf_filtered ("%s", cmd->name); if (alias && (cmd->flags & CMD_DEPRECATED)) printf_filtered ("' (%s) is deprecated.\n", alias->name); else printf_filtered ("' is deprecated.\n"); /* if it is only the alias that is deprecated, we want to indicate the new alias, otherwise we'll indicate the new command */ if (alias && !(cmd->flags & CMD_DEPRECATED)) { if (alias->replacement) printf_filtered ("Use '%s'.\n\n", alias->replacement); else printf_filtered ("No alternative known.\n\n"); } else { if (cmd->replacement) printf_filtered ("Use '%s'.\n\n", cmd->replacement); else printf_filtered ("No alternative known.\n\n"); } /* We've warned you, now we'll keep quiet */ if (alias) alias->flags &= ~DEPRECATED_WARN_USER; cmd->flags &= ~DEPRECATED_WARN_USER;}/* Look up the contents of LINE as a command in the command list 'cmdlist'. Return 1 on success, 0 on failure. If LINE refers to an alias, *alias will point to that alias. If LINE is a postfix command (i.e. one that is preceeded by a prefix command) set *prefix_cmd. Set *cmd to point to the command LINE indicates. If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not exist, they are NULL when we return. */intlookup_cmd_composition (char *text, struct cmd_list_element **alias, struct cmd_list_element **prefix_cmd, struct cmd_list_element **cmd){ char *p, *command; int len, tmp, nfound; struct cmd_list_element *cur_list; struct cmd_list_element *prev_cmd; *alias = NULL; *prefix_cmd = NULL; *cmd = NULL; cur_list = cmdlist; while (1) { /* Go through as many command lists as we need to to find the command TEXT refers to. */ prev_cmd = *cmd; while (*text == ' ' || *text == '\t') (text)++; /* Treating underscores as part of command words is important so that "set args_foo()" doesn't get interpreted as "set args _foo()". */ /* NOTE: cagney/2003-02-13 The `tui_active' was previously `tui_version'. */ for (p = text; *p && (isalnum (*p) || *p == '-' || *p == '_' ||#if defined(TUI) (tui_active && (*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||#endif (xdb_commands && (*p == '!' || *p == '/' || *p == '?'))); p++) ; /* If nothing but whitespace, return. */ if (p == text) return 0; len = p - text; /* text and p now bracket the first command word to lookup (and it's length is len). We copy this into a local temporary */ command = (char *) alloca (len + 1); for (tmp = 0; tmp < len; tmp++) { char x = text[tmp]; command[tmp] = x; } command[len] = '\0'; /* Look it up. */ *cmd = 0; nfound = 0; *cmd = find_cmd (command, len, cur_list, 1, &nfound); /* We didn't find the command in the entered case, so lower case it and search again. */ if (!*cmd || nfound == 0) { for (tmp = 0; tmp < len; tmp++) { char x = command[tmp]; command[tmp] = isupper (x) ? tolower (x) : x; } *cmd = find_cmd (command, len, cur_list, 1, &nfound); } if (*cmd == (struct cmd_list_element *) -1) { return 0; /* ambiguous */ } if (*cmd == NULL) return 0; /* nothing found */ else { if ((*cmd)->cmd_pointer) { /* cmd was actually an alias, we note that an alias was used (by assigning *alais) and we set *cmd. */ *alias = *cmd; *cmd = (*cmd)->cmd_pointer; } *prefix_cmd = prev_cmd; } if ((*cmd)->prefixlist) cur_list = *(*cmd)->prefixlist; else return 1; text = p; }}/* Helper function for SYMBOL_COMPLETION_FUNCTION. *//* Return a vector of char pointers which point to the different possible completions in LIST of TEXT. WORD points in the same buffer as TEXT, and completions should be returned relative to this position. For example, suppose TEXT is "foo" and we want to complete to "foobar". If WORD is "oo", return "oobar"; if WORD is "baz/foo", return "baz/foobar". */char **complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word){ struct cmd_list_element *ptr; char **matchlist; int sizeof_matchlist; int matches; int textlen = strlen (text); sizeof_matchlist = 10; matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *)); matches = 0; for (ptr = list; ptr; ptr = ptr->next) if (!strncmp (ptr->name, text, textlen) && !ptr->abbrev_flag && (ptr->func || ptr->prefixlist)) { if (matches == sizeof_matchlist) { sizeof_matchlist *= 2; matchlist = (char **) xrealloc ((char *) matchlist, (sizeof_matchlist * sizeof (char *))); } matchlist[matches] = (char *) xmalloc (strlen (word) + strlen (ptr->name) + 1); if (word == text) strcpy (matchlist[matches], ptr->name); else if (word > text) { /* Return some portion of ptr->name. */ strcpy (matchlist[matches], ptr->name + (word - text)); } else { /* Return some of text plus ptr->name. */ strncpy (matchlist[matches], word, text - word); matchlist[matches][text - word] = '\0'; strcat (matchlist[matches], ptr->name); } ++matches; } if (matches == 0) { xfree (matchlist); matchlist = 0; } else { matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1) * sizeof (char *))); matchlist[matches] = (char *) 0; } return matchlist;}/* Helper function for SYMBOL_COMPLETION_FUNCTION. *//* Return a vector of char pointers which point to the different possible completions in CMD of TEXT. WORD points in the same buffer as TEXT, and completions should be returned relative to this position. For example, suppose TEXT is "foo" and we want to complete to "foobar". If WORD is "oo", return "oobar"; if WORD is "baz/foo", return "baz/foobar". */char **complete_on_enum (const char *enumlist[], char *text, char *word){ char **matchlist; int sizeof_matchlist; int matches; int textlen = strlen (text); int i; const char *name; sizeof_matchlist = 10; matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *)); matches = 0; for (i = 0; (name = enumlist[i]) != NULL; i++) if (strncmp (name, text, textlen) == 0) { if (matches == sizeof_matchlist) { sizeof_matchlist *= 2; matchlist = (char **) xrealloc ((char *) matchlist, (sizeof_matchlist * sizeof (char *))); } matchlist[matches] = (char *) xmalloc (strlen (word) + strlen (name) + 1); if (word == text) strcpy (matchlist[matches], name); else if (word > text) { /* Return some portion of name. */ strcpy (matchlist[matches], name + (word - text)); } else { /* Return some of text plus name. */ strncpy (matchlist[matches], word, text - word); matchlist[matches][text - word] = '\0'; strcat (matchlist[matches], name); } ++matches; } if (matches == 0) { xfree (matchlist); matchlist = 0; } else { matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1) * sizeof (char *))); matchlist[matches] = (char *) 0; } return matchlist;}/* check function pointer */intcmd_func_p (struct cmd_list_element *cmd){ return (cmd->func != NULL);}/* call the command function */voidcmd_func (struct cmd_list_element *cmd, char *args, int from_tty){ if (cmd_func_p (cmd)) (*cmd->func) (cmd, args, from_tty); else error ("Invalid command");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -