⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 command.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
  /* Check all possibilities in the current command list.  */  found = 0;  nfound = 0;  for (c = list; c; c = c->next)    {      if (!strncmp (processed_cmd, c->name, cmd_len))	{	  found = c;	  nfound++;	  if (c->name[cmd_len] == 0)	    {	      nfound = 1;	      break;	    }	}    }  /* Report error for undefined command name.  */  if (nfound != 1)    {      if (nfound > 1 && allow_unknown >= 0)	{	  ambbuf[0] = 0;	  for (c = list; c; c = c->next)	    if (!strncmp (processed_cmd, c->name, cmd_len))	      {		if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)		  {		    if (strlen (ambbuf))		      strcat (ambbuf, ", ");		    strcat (ambbuf, c->name);		  }		else		  {		    strcat (ambbuf, "..");		    break;		  }	      }	  error ("Ambiguous %scommand \"%s\": %s.", cmdtype,		 processed_cmd, ambbuf);	}      else if (!allow_unknown)	error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);      return 0;    }  /* Skip whitespace before the argument.  */  while (*p == ' ' || *p == '\t') p++;  *line = p;  if (found->prefixlist && *p)    {      c = lookup_cmd (line, *found->prefixlist, found->prefixname,		      found->allow_unknown);      if (c)	return c;    }  return found;}#endif/* Helper function for SYMBOL_COMPLETION_FUNCTION.  *//* Return a vector of char pointers which point to the different   possible completions in LIST of TEXT.  */char **complete_on_cmdlist (list, text)     struct cmd_list_element *list;     char *text;{  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->function.cfunc	    || ptr->prefixlist))      {	if (matches == sizeof_matchlist)	  {	    sizeof_matchlist *= 2;	    matchlist = (char **) xrealloc ((char *)matchlist,					    (sizeof_matchlist					     * sizeof (char *)));	  }	matchlist[matches] = (char *) 	  xmalloc (strlen (ptr->name) + 1);	strcpy (matchlist[matches++], ptr->name);      }  if (matches == 0)    {      free ((PTR)matchlist);      matchlist = 0;    }  else    {      matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)						* sizeof (char *)));      matchlist[matches] = (char *) 0;    }  return matchlist;}static intparse_binary_operation (arg)     char *arg;{  int length;  if (!arg || !*arg)    return 1;  length = strlen (arg);  while (arg[length - 1] == ' ' || arg[length - 1] == '\t')    length--;  if (!strncmp (arg, "on", length)      || !strncmp (arg, "1", length)      || !strncmp (arg, "yes", length))    return 1;  else    if (!strncmp (arg, "off", length)	|| !strncmp (arg, "0", length)	|| !strncmp (arg, "no", length))      return 0;    else       {	error ("\"on\" or \"off\" expected.");	return 0;      }}/* Do a "set" or "show" command.  ARG is NULL if no argument, or the text   of the argument, and FROM_TTY is nonzero if this command is being entered   directly by the user (i.e. these are just like any other   command).  C is the command list element for the command.  */voiddo_setshow_command (arg, from_tty, c)     char *arg;     int from_tty;     struct cmd_list_element *c;{  if (c->type == set_cmd)    {      switch (c->var_type)	{	case var_string:	  {	    char *new;	    char *p;	    char *q;	    int ch;	    	    if (arg == NULL)	      arg = "";	    new = (char *) xmalloc (strlen (arg) + 2);	    p = arg; q = new;	    while ((ch = *p++) != '\000')	      {		if (ch == '\\')		  {		    /* \ at end of argument is used after spaces		       so they won't be lost.  */		    if (*p == 0)		      break;		    ch = parse_escape (&p);		    if (ch == 0)		      break; /* C loses */		    else if (ch > 0)		      *q++ = ch;		  }		else		  *q++ = ch;	      }	    if (*(p - 1) != '\\')	      *q++ = ' ';	    *q++ = '\0';	    new = (char *) xrealloc (new, q - new);	    if (*(char **)c->var != NULL)	      free (*(char **)c->var);	    *(char **) c->var = new;	  }	  break;	case var_string_noescape:	  if (arg == NULL)	    arg = "";	  if (*(char **)c->var != NULL)	    free (*(char **)c->var);	  *(char **) c->var = savestring (arg, strlen (arg));	  break;	case var_filename:	  if (arg == NULL)	    error_no_arg ("filename to set it to.");	  if (*(char **)c->var != NULL)	    free (*(char **)c->var);	  *(char **)c->var = tilde_expand (arg);	  break;	case var_boolean:	  *(int *) c->var = parse_binary_operation (arg);	  break;	case var_uinteger:	  if (arg == NULL)	    error_no_arg ("integer to set it to.");	  *(int *) c->var = parse_and_eval_address (arg);	  if (*(int *) c->var == 0)	    *(int *) c->var = UINT_MAX;	  break;	case var_zinteger:	  if (arg == NULL)	    error_no_arg ("integer to set it to.");	  *(int *) c->var = parse_and_eval_address (arg);	  break;	default:	  error ("gdb internal error: bad var_type in do_setshow_command");	}    }  else if (c->type == show_cmd)    {      /* Print doc minus "show" at start.  */      print_doc_line (stdout, c->doc + 5);            fputs_filtered (" is ", stdout);      wrap_here ("    ");      switch (c->var_type)	{      case var_string:	{	  unsigned char *p;	  fputs_filtered ("\"", stdout);	  for (p = *(unsigned char **) c->var; *p != '\0'; p++)	    printchar (*p, stdout, '"');	  fputs_filtered ("\"", stdout);	}	break;      case var_string_noescape:      case var_filename:	fputs_filtered ("\"", stdout);	fputs_filtered (*(char **) c->var, stdout);	fputs_filtered ("\"", stdout);	break;      case var_boolean:	fputs_filtered (*(int *) c->var ? "on" : "off", stdout);	break;      case var_uinteger:	if (*(unsigned int *) c->var == UINT_MAX) {	  fputs_filtered ("unlimited", stdout);	  break;	}	/* else fall through */      case var_zinteger:	fprintf_filtered (stdout, "%d", *(unsigned int *) c->var);	break;      default:	error ("gdb internal error: bad var_type in do_setshow_command");      }      fputs_filtered (".\n", stdout);    }  else    error ("gdb internal error: bad cmd_type in do_setshow_command");  (*c->function.sfunc) (NULL, from_tty, c);}/* Show all the settings in a list of show commands.  */voidcmd_show_list (list, from_tty, prefix)     struct cmd_list_element *list;     int from_tty;     char *prefix;{  for (; list != NULL; list = list->next) {    /* If we find a prefix, run its list, prefixing our output by its       prefix (with "show " skipped).  */    if (list->prefixlist && !list->abbrev_flag)      cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);    if (list->type == show_cmd)      {	fputs_filtered (prefix, stdout);	fputs_filtered (list->name, stdout);	fputs_filtered (":  ", stdout);	do_setshow_command ((char *)NULL, from_tty, list);      }  }}/* ARGSUSED */static voidshell_escape (arg, from_tty)     char *arg;     int from_tty;{  int rc, status, pid;  char *p, *user_shell;  if ((user_shell = (char *) getenv ("SHELL")) == NULL)    user_shell = "/bin/sh";  /* Get the name of the shell for arg0 */  if ((p = strrchr (user_shell, '/')) == NULL)    p = user_shell;  else    p++;			/* Get past '/' */  if ((pid = fork()) == 0)    {      if (!arg)	execl (user_shell, p, 0);      else	execl (user_shell, p, "-c", arg, 0);      fprintf (stderr, "Exec of shell failed\n");      exit (0);    }  if (pid != -1)    while ((rc = wait (&status)) != pid && rc != -1)      ;  else    error ("Fork failed");}static voidmake_command (arg, from_tty)     char *arg;     int from_tty;{  char *p;  if (arg == 0)    p = "make";  else    {      p = xmalloc (sizeof("make ") + strlen(arg));      strcpy (p, "make ");      strcpy (p + sizeof("make ")-1, arg);    }    shell_escape (p, from_tty);}static voidshow_user_1 (c, stream)     struct cmd_list_element *c;     FILE *stream;{  register struct command_line *cmdlines;  cmdlines = c->user_commands;  if (!cmdlines)    return;  fprintf_filtered (stream, "User command %s:\n", c->name);  while (cmdlines)    {      fprintf_filtered (stream, "%s\n", cmdlines->line);       cmdlines = cmdlines->next;    }  fputs_filtered ("\n", stream);}/* ARGSUSED */static voidshow_user (args, from_tty)     char *args;     int from_tty;{  struct cmd_list_element *c;  extern struct cmd_list_element *cmdlist;  if (args)    {      c = lookup_cmd (&args, cmdlist, "", 0, 1);      if (c->class != class_user)	error ("Not a user command.");      show_user_1 (c, stdout);    }  else    {      for (c = cmdlist; c; c = c->next)	{	  if (c->class == class_user)	    show_user_1 (c, stdout);	}    }}void_initialize_command (){  add_com ("shell", class_support, shell_escape,	   "Execute the rest of the line as a shell command.  \n\With no arguments, run an inferior shell.");  add_com ("make", class_support, make_command,	   "Run the ``make'' program using the rest of the line as arguments.");  add_cmd ("user", no_class, show_user, 	   "Show definitions of user defined commands.\n\Argument is the name of the user defined command.\n\With no argument, show definitions of all user defined commands.", &showlist);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -