Beispiel #1
0
/*****************************************************************************
 函 数 名  : cmd_resolve_quest
 功能描述  : 适配?字符,联想命令
 输入参数  : struct cmd_vty *vty
 输出参数  : 无
 返 回 值  : void
 调用函数  :
 被调函数  :

 修改历史      :
  1.日    期   : 2013年11月17日
    作    者   : weizengke
    修改内容   : 新生成函数

	1: 完全匹配输入时,只返回该命令的联想  2013-10-27 (未实现)

	2:bug:
		 display loopback
		 disable loopback-detect
	   >dis loop   =======> show what

*****************************************************************************/
void cmd_resolve_quest(struct cmd_vty *vty)
{
	cmd_vector_t *v;
	struct para_desc *match[CMD_MAX_MATCH_SIZE];	// matched string
	int match_size = 0;
	int i = 0;
	int nomath_pos = -1;

	/*
	1: 取pos 之前的buf
	2: 需要覆盖当前光标后的buf
	*/
	/* BEGIN: Added by weizengke, 2013/11/17 bug for left and tab*/
	memset(&(vty->buffer[vty->cur_pos]), 0 ,strlen(vty->buffer) - vty->cur_pos);
	vty->used_len = strlen(vty->buffer);
	/* END:   Added by weizengke, 2013/11/17 */

	v = str2vec(vty->buffer);
	if (v == NULL)
	{
		v = cmd_vector_init(1);
		cmd_vector_insert_cr(v);
	}
	else if (isspace((int)vty->buffer[vty->used_len - 1]))
	{
		cmd_vector_insert_cr(v);
	}


	cmd_complete_command(v, vty, match, &match_size, &nomath_pos);

	cmd_outstring("%s", CMD_ENTER);

	if (match_size) {
		for (i = 0; i < match_size; i++) {
			cmd_outstring(" %-25s%s\r\n", match[i]->para,match[i]->desc);
		}
		cmd_outprompt(vty->prompt);
		cmd_outstring("%s", vty->buffer);
	} else {

		cmd_output_missmatch(vty, nomath_pos);

		cmd_outprompt(vty->prompt);
		cmd_outstring("%s", vty->buffer);
	}

	cmd_vector_deinit(v, 0);
}
Beispiel #2
0
int cmd_init()
{
	/* initial cmd vector */
	cmd_vec = cmd_vector_init(1);

	/* install cmd */
	cmd_install();

	/* initial vty */
	vty = cmd_vty_init();
	if (vty == NULL)
	{
		return OS_ERR;
	}

	return OS_OK;
}
Beispiel #3
0
cmd_vector_t *cmd2vec(char *string, char *doc)
{
	char *sp=NULL, *d_sp=NULL;
	char *cp = string;
	char *d_cp = doc;
	char *token=NULL, *d_token=NULL;
	int len, d_len;

	cmd_vector_t *allvec=NULL;
	struct para_desc *desc = NULL;
	struct para_desc *desc_cr = NULL;

	if(cp == NULL)
	{
		return NULL;
	}

	allvec = cmd_vector_init(1);
	while (1)
	{
		while(isspace((int) *cp) && *cp != '\0')
		{
			cp++;
		}

		if(*cp == '\0')
		{
			break;
		}

		sp = cp;

		while(!(isspace ((int) *cp) || *cp == '\r' || *cp == '\n')
			  && *cp != '\0')
		{
			cp++;
		}

		len = cp - sp;
		token = (char*)malloc(len + 1);
		if (NULL == token)
		{
			debug_print_ex(CMD_DEBUG_TYPE_ERROR, "In cmd2vec, There is no memory for param token.");
			return NULL;
		}

		memcpy(token, sp, len);
		*(token + len) = '\0';

		while(isspace((int)*d_cp) && *d_cp != '\0')
		{
			d_cp++;
		}

		if (*d_cp == '\0')
		{
			d_token = NULL;
		}
		else
		{
			d_sp = d_cp;

			while(!(*d_cp == '\r' || *d_cp == '\n') && *d_cp != '\0')
			{
				d_cp++;
			}

			d_len = d_cp - d_sp;
			d_token = (char*)malloc(d_len + 1);
			if (NULL == d_token)
			{
				debug_print_ex(CMD_DEBUG_TYPE_ERROR, "In cmd2vec, There is no memory for param d_token.");
				free(token);
				return NULL;
			}

			memcpy(d_token, d_sp, d_len);
			*(d_token + d_len) = '\0';
		}

		desc = (struct para_desc *)calloc(1, sizeof(struct para_desc));
		if (desc == NULL)
		{
			debug_print_ex(CMD_DEBUG_TYPE_ERROR, "In cmd2Vec, calloc for desc fail. (token=%s)", token);
			free(token);
			free(d_token);
			return NULL;
		}

		/* BEGIN: Added by weizengke, 2013/10/4   PN:for regCmdElem  */
		if (0 != cmd_get_elem_by_name(token, desc))
		{
			debug_print_ex(CMD_DEBUG_TYPE_ERROR, "In cmd2Vec, cmd_get_elem_by_name fail. (token=%s)", token);
			free(token);
			free(d_token);
			free(desc);
			return NULL;
		}
		/* END:   Added by weizengke, 2013/10/4   PN:None */

		cmd_vector_insert(allvec, (void *)desc);
	}

	/* add <CR> into command vector */
	desc_cr = (struct para_desc *)calloc(1, sizeof(struct para_desc));
	if (desc_cr == NULL)
	{
		debug_print_ex(CMD_DEBUG_TYPE_ERROR, "In cmd2Vec, calloc for desc_cr fail. (token=%s)", token);
		cmd_vector_deinit(allvec, 1);
		return NULL;
	}

	/* BEGIN: Added by weizengke, 2013/10/4   PN:for regCmdElem  */
	if (0 != cmd_get_elem_by_name((char*)CMD_END, desc_cr))
	{
		debug_print_ex(CMD_DEBUG_TYPE_ERROR, "In cmd2Vec, cmd_get_elem_by_name fail. (token=%s)", token);
		free(desc_cr);
		cmd_vector_deinit(allvec, 1);
		return NULL;
	}
	/* END:   Added by weizengke, 2013/10/4   PN:None */

	cmd_vector_insert(allvec, (void *)desc_cr);

/*
	{
		struct para_desc *desc_ = NULL;
		for (int i = 0; i < cmd_vector_max(allvec); i++)
		{
			desc_ = (struct para_desc *)cmd_vector_slot(allvec, i);
			printf("cmd2vec + %s\n",(char*)desc_->para);
		}
	}
*/
	return allvec;
}
Beispiel #4
0
cmd_vector_t *str2vec(char *string)
{
	int str_len;
	char *cur, *start, *token;
	cmd_vector_t *vec;

	// empty string
	if (string == NULL)
	{
		return NULL;
	}

	cur = string;
	// skip white spaces
	while (isspace((int) *cur) && *cur != '\0')
	{
		cur++;
	}

	// only white spaces
	if (*cur == '\0')
	{
		return NULL;
	}

	// copy each command pieces into vector
	vec = cmd_vector_init(1);
	while (1)
	{
		start = cur;
		while (!(isspace((int) *cur) || *cur == '\r' || *cur == '\n') &&
				*cur != '\0')
		{
			cur++;
		}

		str_len = cur - start;
		token = (char *)malloc(sizeof(char) * (str_len + 1));
		if (NULL == token)
		{
			debug_print_ex(CMD_DEBUG_TYPE_ERROR, "In str2vec, There is no memory for param token.");
			return NULL;
		}

		memcpy(token, start, str_len);
		*(token + str_len) = '\0';
		cmd_vector_insert(vec, (void *)token);

		while((isspace ((int) *cur) || *cur == '\n' || *cur == '\r') &&
			*cur != '\0')
		{
			cur++;
		}

		if (*cur == '\0')
		{
			return vec;
		}

	}
}