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

📄 command.c

📁 大名鼎鼎的路由器源码。程序分ZEBRA、OSPFRIP等3个包。程序框架采用一个路由协议一个进程的方式
💻 C
📖 第 1 页 / 共 5 页
字号:
      command = vector_slot (vline, index);      match = cmd_filter_by_completion (command, cmd_vector, index);      if (match == vararg_match)	break;      ret = is_cmd_ambiguous (command, cmd_vector, index, match);      if (ret == 1)	{	  vector_free (cmd_vector);	  return CMD_ERR_AMBIGUOUS;	}      else if (ret == 2)	{	  vector_free (cmd_vector);	  return CMD_ERR_NO_MATCH;	}    }  /* Check matched count. */  matched_element = NULL;  matched_count = 0;  incomplete_count = 0;  for (i = 0; i < vector_max (cmd_vector); i++)     if (vector_slot (cmd_vector,i) != NULL)      {	cmd_element = vector_slot (cmd_vector,i);	if (match == vararg_match || index >= cmd_element->cmdsize)	  {	    matched_element = cmd_element;#if 0	    printf ("DEBUG: %s\n", cmd_element->string);#endif	    matched_count++;	  }	else	  {	    incomplete_count++;	  }      }    /* Finish of using cmd_vector. */  vector_free (cmd_vector);  /* To execute command, matched_count must be 1.*/  if (matched_count == 0)     {      if (incomplete_count)	return CMD_ERR_INCOMPLETE;      else	return CMD_ERR_NO_MATCH;    }  if (matched_count > 1)     return CMD_ERR_AMBIGUOUS;  /* Argument treatment */  varflag = 0;  argc = 0;  for (i = 0; i < vector_max (vline); i++)    {      if (varflag)	argv[argc++] = vector_slot (vline, i);      else	{	  	  vector descvec = vector_slot (matched_element->strvec, i);	  if (vector_max (descvec) == 1)	    {	      struct desc *desc = vector_slot (descvec, 0);	      char *str = desc->cmd;	      if (CMD_VARARG (str))		varflag = 1;	      if (varflag || CMD_VARIABLE (str) || CMD_OPTION (str))		argv[argc++] = vector_slot (vline, i);	    }	  else	    argv[argc++] = vector_slot (vline, i);	}      if (argc >= CMD_ARGC_MAX)	return CMD_ERR_EXEED_ARGC_MAX;    }  /* For vtysh execution. */  if (cmd)    *cmd = matched_element;  if (matched_element->daemon)    return CMD_SUCCESS_DAEMON;  /* Execute matched command. */  return (*matched_element->func) (matched_element, vty, argc, argv);}/* Execute command by argument readline. */intcmd_execute_command_strict (vector vline, struct vty *vty, 			    struct cmd_element **cmd){  int i;  int index;  vector cmd_vector;  struct cmd_element *cmd_element;  struct cmd_element *matched_element;  unsigned int matched_count, incomplete_count;  int argc;  char *argv[CMD_ARGC_MAX];  int varflag;  enum match_type match = 0;  char *command;  /* Make copy of command element */  cmd_vector = vector_copy (cmd_node_vector (cmdvec, vty->node));  for (index = 0; index < vector_max (vline); index++)     {      int ret;      command = vector_slot (vline, index);      match = cmd_filter_by_string (vector_slot (vline, index), 				    cmd_vector, index);      /* If command meets '.VARARG' then finish matching. */      if (match == vararg_match)	break;      ret = is_cmd_ambiguous (command, cmd_vector, index, match);      if (ret == 1)	{	  vector_free (cmd_vector);	  return CMD_ERR_AMBIGUOUS;	}      if (ret == 2)	{	  vector_free (cmd_vector);	  return CMD_ERR_NO_MATCH;	}    }  /* Check matched count. */  matched_element = NULL;  matched_count = 0;  incomplete_count = 0;  for (i = 0; i < vector_max (cmd_vector); i++)     if (vector_slot (cmd_vector,i) != NULL)      {	cmd_element = vector_slot (cmd_vector,i);	if (match == vararg_match || index >= cmd_element->cmdsize)	  {	    matched_element = cmd_element;	    matched_count++;	  }	else	  incomplete_count++;      }    /* Finish of using cmd_vector. */  vector_free (cmd_vector);  /* To execute command, matched_count must be 1.*/  if (matched_count == 0)     {      if (incomplete_count)	return CMD_ERR_INCOMPLETE;      else	return CMD_ERR_NO_MATCH;    }  if (matched_count > 1)     return CMD_ERR_AMBIGUOUS;  /* Argument treatment */  varflag = 0;  argc = 0;  for (i = 0; i < vector_max (vline); i++)    {      if (varflag)	argv[argc++] = vector_slot (vline, i);      else	{	  	  vector descvec = vector_slot (matched_element->strvec, i);	  if (vector_max (descvec) == 1)	    {	      struct desc *desc = vector_slot (descvec, 0);	      char *str = desc->cmd;	      if (CMD_VARARG (str))		varflag = 1;	  	      if (varflag || CMD_VARIABLE (str) || CMD_OPTION (str))		argv[argc++] = vector_slot (vline, i);	    }	  else	    argv[argc++] = vector_slot (vline, i);	}      if (argc >= CMD_ARGC_MAX)	return CMD_ERR_EXEED_ARGC_MAX;    }  /* For vtysh execution. */  if (cmd)    *cmd = matched_element;  if (matched_element->daemon)    return CMD_SUCCESS_DAEMON;  /* Now execute matched command */  return (*matched_element->func) (matched_element, vty, argc, argv);}/* Configration make from file. */intconfig_from_file (struct vty *vty, FILE *fp){  int ret;  vector vline;  while (fgets (vty->buf, VTY_BUFSIZ, fp))    {      vline = cmd_make_strvec (vty->buf);      /* In case of comment line */      if (vline == NULL)	continue;      /* Execute configuration command : this is strict match */      ret = cmd_execute_command_strict (vline, vty, NULL);      /* Try again with setting node to CONFIG_NODE */      if (ret != CMD_SUCCESS && ret != CMD_WARNING)	{	  if (vty->node == KEYCHAIN_KEY_NODE)	    {	      vty->node = KEYCHAIN_NODE;	      ret = cmd_execute_command_strict (vline, vty, NULL);	      if (ret != CMD_SUCCESS && ret != CMD_WARNING)		{		  vty->node = CONFIG_NODE;		  ret = cmd_execute_command_strict (vline, vty, NULL);		}	    }	  else	    {	      vty->node = CONFIG_NODE;	      ret = cmd_execute_command_strict (vline, vty, NULL);	    }	}	        cmd_free_strvec (vline);      if (ret != CMD_SUCCESS && ret != CMD_WARNING)	return ret;    }  return CMD_SUCCESS;}/* Configration from terminal */DEFUN (config_terminal,       config_terminal_cmd,       "configure terminal",       "Configuration from vty interface\n"       "Configuration terminal\n"){  if (vty_config_lock (vty))    vty->node = CONFIG_NODE;  else    {      vty_out (vty, "VTY configuration is locked by other VTY%s", VTY_NEWLINE);      return CMD_WARNING;    }  return CMD_SUCCESS;}/* Enable command */DEFUN (enable,        config_enable_cmd,       "enable",       "Turn on privileged mode command\n"){  /* If enable password is NULL, change to ENABLE_NODE */  if ((host.enable == NULL && host.enable_encrypt == NULL) ||      vty->type == VTY_SHELL_SERV)    vty->node = ENABLE_NODE;  else    vty->node = AUTH_ENABLE_NODE;  return CMD_SUCCESS;}/* Disable command */DEFUN (disable,        config_disable_cmd,       "disable",       "Turn off privileged mode command\n"){  if (vty->node == ENABLE_NODE)    vty->node = VIEW_NODE;  return CMD_SUCCESS;}/* Down vty node level. */DEFUN (config_exit,       config_exit_cmd,       "exit",       "Exit current mode and down to previous mode\n"){  switch (vty->node)    {    case VIEW_NODE:    case ENABLE_NODE:      if (vty_shell (vty))	exit (0);      else	vty->status = VTY_CLOSE;      break;    case CONFIG_NODE:      vty->node = ENABLE_NODE;      vty_config_unlock (vty);      break;    case INTERFACE_NODE:    case ZEBRA_NODE:    case BGP_NODE:    case RIP_NODE:    case RIPNG_NODE:    case OSPF_NODE:    case OSPF6_NODE:    case KEYCHAIN_NODE:    case MASC_NODE:    case RMAP_NODE:    case VTY_NODE:      vty->node = CONFIG_NODE;      break;    case BGP_VPNV4_NODE:    case BGP_IPV4_NODE:    case BGP_IPV4M_NODE:    case BGP_IPV6_NODE:      vty->node = BGP_NODE;      break;    case KEYCHAIN_KEY_NODE:      vty->node = KEYCHAIN_NODE;      break;    default:      break;    }  return CMD_SUCCESS;}/* quit is alias of exit. */ALIAS (config_exit,       config_quit_cmd,       "quit",       "Exit current mode and down to previous mode\n");       /* End of configuration. */DEFUN (config_end,       config_end_cmd,       "end",       "End current mode and change to enable mode."){  switch (vty->node)    {    case VIEW_NODE:    case ENABLE_NODE:      /* Nothing to do. */      break;    case CONFIG_NODE:    case INTERFACE_NODE:    case ZEBRA_NODE:    case RIP_NODE:    case RIPNG_NODE:    case BGP_NODE:    case BGP_VPNV4_NODE:    case BGP_IPV4_NODE:    case BGP_IPV4M_NODE:    case BGP_IPV6_NODE:    case RMAP_NODE:    case OSPF_NODE:    case OSPF6_NODE:    case KEYCHAIN_NODE:    case KEYCHAIN_KEY_NODE:    case MASC_NODE:    case VTY_NODE:      vty_config_unlock (vty);      vty->node = ENABLE_NODE;      break;    default:      break;    }  return CMD_SUCCESS;}/* Show version. */DEFUN (show_version,       show_version_cmd,       "show version",       SHOW_STR       "Displays zebra version\n"){  vty_out (vty, "Zebra %s (%s).%s", ZEBRA_VERSION,	   host_name,	   VTY_NEWLINE);  vty_out (vty, "Copyright 1996-2002, Kunihiro Ishiguro.%s", VTY_NEWLINE);  return CMD_SUCCESS;}/* Help display function for all node. */DEFUN (config_help,       config_help_cmd,       "help",       "Description of the interactive help system\n"){  vty_out (vty, 	   "Zebra VTY provides advanced help feature.  When you need help,%s\anytime at the command line please press '?'.%s\%s\If nothing matches, the help list will be empty and you must backup%s\ until entering a '?' shows the available options.%s\Two styles of help are provided:%s\1. Full help is available when you are ready to enter a%s\command argument (e.g. 'show ?') and describes each possible%s\argument.%s\2. Partial help is provided when an abbreviated argument is entered%s\   and you want to know what arguments match the input%s\   (e.g. 'show me?'.)%s%s", VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,	   VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,	   VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);  return CMD_SUCCESS;}/* Help display function for all node. */DEFUN (config_list,       config_list_cmd,       "list",       "Print command list\n"){  int i;  struct cmd_node *cnode = vector_slot (cmdvec, vty->node);  struct cmd_element *cmd;  for (i = 0; i < vector_max (cnode->cmd_vector); i++)    if ((cmd = vector_slot (cnode->cmd_vector, i)) != NULL)      vty_out (vty, "  %s%s", cmd->string,	       VTY_NEWLINE);  return CMD_SUCCESS;}/* Write current configuration into file. */DEFUN (config_write_file,        config_write_file_cmd,       "write file",         "Write running configuration to memory, network, or terminal\n"       "Write to configuration file\n"){  int i;  int fd;  struct cmd_node *node;  char *config_file;  char *config_file_tmp = NULL;  char *config_file_sav = NULL;  struct vty *file_vty;  /* Check and see if we are operating under vtysh configuration */  if (host.config == NULL)    {      vty_out (vty, "Can't save to configuration file, using vtysh.%s",	       VTY_NEWLINE);      return CMD_WARNING;    }  /* Get filename. */  config_file = host.config;    config_file_sav = malloc (strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1);  strcpy (config_file_sav, config_file);  strcat (config_file_sav, CONF_BACKUP_EXT);  config_file_tmp = malloc (strlen (config_file) + 8);  sprintf (config_file_tmp, "%s.XXXXXX", config_file);    /* Open file to configuration write. */  fd = mkstemp (config_file_tmp);  if (fd < 0)    {      vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp,	       VTY_NEWLINE);      free (config_file_tmp);      free (config_file_sav);      return CMD_WARNING;    }    /* Make vty for configuration file. */  file_vty = vty_new ();  file_vty->fd = fd;  file_vty->type = VTY_FILE;  /* Config file header print. */  vty_out (file_vty, "!\n! Zebra configuration saved from vty\n!   ");  vty_time_print (file_vty, 1);  vty_out (file_vty, "!\n");  for (i = 0; i < vector_max (cmdvec); i++)    if ((node = vector_slot (cmdvec, i)) && node->func)      {	if ((*node->func) (file_vty))	  vty_out (file_vty, "!\n");      }  vty_close (file_vty);  if (unlink (config_file_sav) != 0)    if (errno != ENOENT)      {	vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav,		 VTY_NEWLINE);	free (config_file_sav);	free (config_file_tmp);	unlink (config_file_tmp);		return CMD_WARNING;      }  if (link (config_file, config_file_sav) != 0)    {      vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav,	        VTY_NEWLINE);      free (config_file_sav);      free (config_file_tmp);      unlink (config_file_tmp);      return CMD_WARNING;    }  sync ();  if (unlink (config_file) != 0)    {      vty_out (vty, "Can't unlink configuration file %s.%s", config_file,	        VTY_NEWLINE);      free (config_file_sav);      free (config_file_tmp);      unlink (config_file_tmp);      return CMD_WARNING;          }  if (link (config_file_tmp, config_file) != 0)    {      vty_out (vty, "Can't save configuration file %s.%s", config_file,	       VTY_NEWLINE);      free (config_file_sav);      free (config_file_tmp);      unlink (config_file_tmp);      return CMD_WARNING;          }  unlink (config_file_tmp);  sync ();    free (config_file_sav);  free (config_file_tmp);  vty_out (vty, "Configuration saved to %s%s", config_file,	   VTY_NEWLINE);  return CMD_SUCCESS;}ALIAS (config_write_file,        config_write_cmd,       "write",         "Write running configuration to memory, network, or terminal\n");ALIAS (config_write_file,        config_write_memory_cmd,       "write memory",         "Write running configuration to memory, network, or terminal\n"       "Write configuration to the file (same as write file)\n");ALIAS (config_write_file,        copy_runningconfig_startupconfig_cmd,       "copy running-config startup-config",         "Copy configuration\n"       "Copy running config to... \n"       "Copy running config to startup config (same as write file)\n");/* Write current configuration into the terminal. */DEFUN (config_write_terminal,       config_write_terminal_cmd,       "write terminal",       "Write running configuration to memory, network, or terminal\n"       "Write to terminal\n"){  int i;  struct cmd_node *node;  if (vty->type == VTY_SHELL_SERV)    {      for (i = 0; i < vector_max (cmdvec); i++)	if ((node = vector_slot (cmdvec, i)) && node->func && node->vtysh)	  {

⌨️ 快捷键说明

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