www.pudn.com > MT7681_IoT_Package_v0.02.rar > iot_at_cmd_utility.c, change:2014-01-28,size:2614b


#include <stdio.h>
#include "string.h"
#include "types.h"
#include "uart.h"
#include "iot_custom.h"


/******************************************************************************
* MODULE NAME:     iot_at_cmd_utility.c
* PROJECT CODE:    __MT7681__
* DESCRIPTION:     
* DESIGNER:        
* DATE:            Jan 2013
*
* SOURCE CONTROL:
*
* LICENSE:
*     This source code is copyright (c) 2011 Ralink Tech. Inc.
*     All rights reserved.
*
* REVISION     HISTORY:
*   V1.0.0     Jan 2012    - Initial Version V1.0
*
*
* SOURCE:
* ISSUES:
*    First Implementation.
* NOTES TO USERS:
*
******************************************************************************/


char * strchr(const char *str, int ch)
{
   while (*str && *str != (char)ch)
        str++;
   if (*str == (char)ch)
        return((char *)str);
   return(NULL);
}

void split_string_cmd(PCHAR string_cmd, INT16 cmdlen, INT16 *pargc, char **argv)
{
	//int cmdlen = (int)strlen(string_cmd);
	argv[0] = (char *)&string_cmd[0];
	INT16 argc = 1;
	INT16 i=0;
	//printf("split_string_cmd1:%s\n", string_cmd);

	for(i=0; i< cmdlen-1;i++)
	{
		if( (string_cmd[i] == '-' || string_cmd[i] == '/') && 
		    (string_cmd[i+1]<='0' || string_cmd[i+1]>='9') )
		{
			string_cmd[i-1]='\0';
			argv[argc] = (char *)&string_cmd[i];
			argc++;
		}
	}
	*pargc=argc;

	//printf("split_string_cmd2:%s,%s,%s,%s\n", argv[0], argv[1],argv[2],argv[3]);
	return;
}

//#define ERR(s, c) if (opterr) 	DBGPRINT(RT_DEBUG_ERROR,("%s: %s%cn", argv[0], s, c));
//int opterr = 1; 
int16 optind = 1; 
char *optarg = NULL; 

int16 getopt(int16 argc, char **argv, char *opts)
{ 
	static int16 sp = 1; 
	register int16 c; 
	register char *cp; 
	if(sp == 1) {
		if (optind >= argc ||
			(argv[optind][0] != '-' && argv[optind][0] != '/' )||
			argv[optind][1] == '\0') 
			return(EOF); 
		else if(!memcmp(argv[optind], "--",2)) {
		//else if(strcmp(argv[optind], "--") == 0) { 
			optind++; 
			return(EOF); 
		} 
	}

	//optopt = c = argv[optind][sp]; 
	c = argv[optind][sp]; 
	
	if(c == ':' || (cp=strchr(opts, c)) == 0) { 
		DBGPRINT(RT_DEBUG_ERROR,("illegal option: %s %c \n", argv[0], c));
		if(argv[optind][++sp] == '\0') { 
			optind++; 
			sp = 1; 
		} 
		return('?'); 
	} 
	if(*++cp == ':') { 
		if(argv[optind][sp+1] != '\0') 
			optarg = &argv[optind++][sp+1]; 
		else if(++optind >= argc) { 
			DBGPRINT(RT_DEBUG_ERROR,("option requires an argument: %s %c \n", argv[0], c));
			sp = 1; 
			return('?'); 
		} else 
			optarg = argv[optind++]; 
		sp = 1; 
	} else { 
		if(argv[optind][++sp] == '\0') { 
			sp = 1; 
			optind++; 
		} 
		optarg = NULL; 
	} 
	return(c); 
}