Ejemplo n.º 1
0
int tcpip_getIp(lua_State *L)
{
char *ret = gprs_ReadCommand(CIFSR, EOL, EOL, L);
char *ptr;
int i = 0;
	ptr = c_strstr(ret,".");
	while( ptr != NULL )
	{
		i++;
		ptr = c_strstr(ptr+1,".");
	}
	if( i == 3 )
		return 1;

return 0;
}
Ejemplo n.º 2
0
char *readCommand(const char *COMMAND, const char *EOM1, const char *EOM2, const char *STARTCOMP, const char *ENDCOMP, lua_State *L)
{
size_t i;
uint32_t now = 0;
char ret[BUFFER_SZ];
char *ptr;
char *end;
size_t TIMEOUT = luaL_checkinteger(L, 1);
int min_sz = 0;
//size_t echo = luaL_checkinteger(L, 2);

	if( COMMAND != NULL )
		prtn(COMMAND, strlen(COMMAND));

	now = system_get_time() + (TIMEOUT * 1000000);
	i = 0;
	min_sz = strlen(EOM1);
	c_memset(ret, 0 , BUFFER_SZ);
	while( now > system_get_time() )
	{
		system_soft_wdt_feed ();
		if( uart_getc(&ret[i & BUFFER_MSK]) )
		{
			if( ret[i] == 0 )
				i--;
			i++;
			if( i >= min_sz )
			{
				if( c_strstr(ret,EOM1) != NULL  || c_strstr(ret,EOM2) != NULL)
				{
					if( (ptr = c_strstr(ret,STARTCOMP)) != NULL )
					{
						ptr += strlen(STARTCOMP);
						if( (end = c_strstr(ptr,ENDCOMP)) != NULL )
						{
							*end = 0;
							return ptr;
						}
					}else
						break;
				}
			}
		}
	}
	return NULL;
}
Ejemplo n.º 3
0
void readFrame(const char *Eof,const char *Eom, const char *Som, char *RET,unsigned int SZ, size_t TIMEOUT)
{
uint32_t now = 0;
char *ptr;
char *end;
int min_sz = 0;
size_t i = 0;
char ret[BUFFER_SZ];

	if( Eof != NULL && Eom != NULL && Som != NULL && RET != NULL && SZ != 0)
	{
		now = system_get_time() + (TIMEOUT * 1000000);
		min_sz = strlen(Eof);
		c_memset(RET, 0 , SZ);
		while( now > system_get_time() )
		{
			system_soft_wdt_feed ();
			if( uart_getc(&ret[i & 0x1FF]) )
			{
				if( ret[i] == 0 )
					i--;
				i++;
				if( i >= min_sz )
				{
					if( c_strstr(ret,Eof) != NULL )
					{
						if( (ptr = c_strstr(ret,Som)) != NULL )
						{
							ptr += strlen(Som);
							if( (end = c_strstr(ptr,Eom)) != NULL )
							{
								*end = 0;
								c_strcpy(RET,ptr);
								return;
							}
						}else
							return;
					}
				}
			}
		}
	}
}
Ejemplo n.º 4
0
int tcpip_getIpStatus(lua_State *L)
{
char *ret = gprs_ReadCommand(IP_STATUS, IP_ANSWER, MSG_OK, L);
int i;
	for(i = 2; i >= 0; i--)
	{
		if( c_strstr(ret,IP_ANS[i]) != NULL)
			return i;
	}
return -1;
}
Ejemplo n.º 5
0
LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
                                                               const char *r) {
  const char *wild;
  size_t l = c_strlen(p);
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  while ((wild = c_strstr(s, p)) != NULL) {
    luaL_addlstring(&b, s, wild - s);  /* push prefix */
    luaL_addstring(&b, r);  /* push replacement in place of pattern */
    s = wild + l;  /* continue after `p' */
  }
  luaL_addstring(&b, s);  /* push last suffix */
  luaL_pushresult(&b);
  return lua_tostring(L, -1);
}
Ejemplo n.º 6
0
int waitAnswer(uint32_t TIMEOUT, const char *MSG_TRUE, const char *MSG_FALSE)
{
uint32_t now = 0;
char ret[BUFFER_SZ];
int i = 0;
	c_memset(ret,0,BUFFER_SZ);
	now = system_get_time() + TIMEOUT;
	while( now > system_get_time() )
	{
		system_soft_wdt_feed ();
		if( uart_getc(&ret[i & BUFFER_MSK]) )
		{
			if( ret[i] == 0 )
				i--;

			i++;
			if( i >= c_strlen(MSG_TRUE) || i >= c_strlen(MSG_FALSE) )
			{
				milisec(1);
				if( STRCMP((char*)MSG_TRUE, (char*)HTTP_OK) != 0 )
				{
					if( c_strstr(ret,MSG_TRUE) != NULL )
						return 1;
					else if( c_strstr(ret,MSG_FALSE) != NULL )
						return 2;
				}else
				{
					if( c_strstr(ret,HTTP_ANSWER[0]) != NULL )
						return 1;
					else if( c_strstr(ret,HTTP_ANSWER[1]) != NULL )
						return 3;
					else if( c_strstr(ret,HTTP_ANSWER[2]) != NULL )
						return 4;
					else if( c_strstr(ret,HTTP_ANSWER[3]) != NULL )
						return 5;
					else if( c_strstr(ret,HTTP_ANSWER[4]) != NULL )
						return 6;
					else if( c_strstr(ret,MSG_FALSE) != NULL )
						return 2;
				}
			}
		}
	}
	milisec(1);
	return 0;
}
Ejemplo n.º 7
0
void
po_lex_charset_set (const char *header_entry, const char *filename)
{
  /* Verify the validity of CHARSET.  It is necessary
     1. for the correct treatment of multibyte characters containing
        0x5C bytes in the PO lexer,
     2. so that at run time, gettext() can call iconv() to convert
        msgstr.  */
  const char *charsetstr = c_strstr (header_entry, "charset=");

  if (charsetstr != NULL)
    {
      size_t len;
      char *charset;
      const char *canon_charset;

      charsetstr += strlen ("charset=");
      len = strcspn (charsetstr, " \t\n");
      charset = (char *) xmalloca (len + 1);
      memcpy (charset, charsetstr, len);
      charset[len] = '\0';

      canon_charset = po_charset_canonicalize (charset);
      if (canon_charset == NULL)
        {
          /* Don't warn for POT files, because POT files usually contain
             only ASCII msgids.  */
          size_t filenamelen = strlen (filename);

          if (!(filenamelen >= 4
                && memcmp (filename + filenamelen - 4, ".pot", 4) == 0
                && strcmp (charset, "CHARSET") == 0))
            {
              char *warning_message =
                xasprintf (_("\
Charset \"%s\" is not a portable encoding name.\n\
Message conversion to user's charset might not work.\n"),
                           charset);
              po_xerror (PO_SEVERITY_WARNING, NULL,
                         filename, (size_t)(-1), (size_t)(-1), true,
                         warning_message);
              free (warning_message);
            }
Ejemplo n.º 8
0
/*
 *      0       1          2     3      4     5 6 7  8    9   10  11 12 13 14
	 $GNGGA,121820.000,2524.7126,S,04917.5765,W,1,8,1.51,983.0,M,0.0,M,,*73
*/
int parse_gps(char *position, double *LAT, double *LONG, double *ALT, double *DOP)
{
char *ptrEnd;
char *ptrStart;
char splited[15][12];

	if( (ptrStart = c_strstr(position,GPSRDRESP)) != NULL)
	{
		//ptrEnd = ptrStart + strlen(ptrStart) - 1;
		c_memset(splited,0,180);

		if( split_pos(ptrStart,&splited[0]) != 0 )
		{
			if( splited[2][0] != 0 && splited[4][0] != 0 && splited[9][0] != 0)
			{
				ptrStart	= &splited[2][0];
				ptrEnd		= &splited[2][strlen(splited[2])];
				if( (*LAT = convDECMIN( ptrStart, ptrEnd - 1 )) != -1.0f )
				{
					if( splited[3][0] == 'S' )
						*LAT = -(*LAT);

					ptrStart	= &splited[4][0];
					ptrEnd		= &splited[4][strlen(splited[2])];
					if( (*LONG = convDECMIN( ptrStart, ptrEnd - 1 )) != -1.0f )
					{
						if( splited[5][0] == 'W' )
							*LONG = -(*LONG);
					}
					*DOP = atof(splited[8]);
					*ALT = atof(splited[9]);
					return 4;
				}
			}
		}
	}
	return 0;
}
Ejemplo n.º 9
0
int
main ()
{
  {
    const char input[] = "foo";
    const char *result = c_strstr (input, "");
    ASSERT (result == input);
  }

  {
    const char input[] = "foo";
    const char *result = c_strstr (input, "o");
    ASSERT (result == input + 1);
  }

  {
    const char input[] = "ABC ABCDAB ABCDABCDABDE";
    const char *result = c_strstr (input, "ABCDABD");
    ASSERT (result == input + 15);
  }

  {
    const char input[] = "ABC ABCDAB ABCDABCDABDE";
    const char *result = c_strstr (input, "ABCDABE");
    ASSERT (result == NULL);
  }

  /* Check that a very long haystack is handled quickly if the needle is
     short and occurs near the beginning.  */
  {
    size_t repeat = 10000;
    size_t m = 1000000;
    const char *needle =
      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    char *haystack = (char *) malloc (m + 1);
    if (haystack != NULL)
      {
        memset (haystack, 'A', m);
        haystack[0] = 'B';
        haystack[m] = '\0';

        for (; repeat > 0; repeat--)
          {
            ASSERT (c_strstr (haystack, needle) == haystack + 1);
          }

        free (haystack);
      }
  }

  /* Check that a very long needle is discarded quickly if the haystack is
     short.  */
  {
    size_t repeat = 10000;
    size_t m = 1000000;
    const char *haystack =
      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
      "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
    char *needle = (char *) malloc (m + 1);
    if (needle != NULL)
      {
        memset (needle, 'A', m);
        needle[m] = '\0';

        for (; repeat > 0; repeat--)
          {
            ASSERT (c_strstr (haystack, needle) == NULL);
          }

        free (needle);
      }
  }

  /* Check that the asymptotic worst-case complexity is not quadratic.  */
  {
    size_t m = 1000000;
    char *haystack = (char *) malloc (2 * m + 2);
    char *needle = (char *) malloc (m + 2);
    if (haystack != NULL && needle != NULL)
      {
        const char *result;

        memset (haystack, 'A', 2 * m);
        haystack[2 * m] = 'B';
        haystack[2 * m + 1] = '\0';

        memset (needle, 'A', m);
        needle[m] = 'B';
        needle[m + 1] = '\0';

        result = c_strstr (haystack, needle);
        ASSERT (result == haystack + m);
      }
    free (needle);
    free (haystack);
  }

  return 0;
}
Ejemplo n.º 10
0
void xscBuildSymbols(txMachine* the)
{
	xsBuffer* aBuffer = xsGetContext(the);
	xsSymbolsData* data = NULL;
	FILE* aFile = NULL;
	txInteger aSize;
	txString aString = NULL;
	txID aCount;
	txID anID;
	txSlot* aSymbol;
	txSize aModulo;
	txSlot* result;
	txID anIndex;
	txByte* aCode;
	txInteger aLength;
	txBoolean failed;
	static char* prefix = "#define xsID_";
	static char* suffix = " (the->code[";
	txInteger prefixLength = c_strlen(prefix);
	txInteger suffixLength = c_strlen(suffix);
	
	mxTry(the) {
		data = c_calloc(1, sizeof(xsSymbolsData));
		xsElseError(data);

		xsVar(1) = xsCat2(xsVar(0), xsString(".h"));
		aFile = fopen(xsToString(xsVar(1)), "rb");
		if (aFile) {
			fseek(aFile, 0, SEEK_END);
			aSize = ftell(aFile);
			rewind(aFile);
			aString = c_malloc(aSize + 1);
			xsElseError(aString);
			fread(aString, 1, aSize, aFile);
			aString[aSize] = 0;
			fclose(aFile);
			aFile = C_NULL;
		}
		
		aCount = 0;
		if (aString) {
			txString p = c_strstr(aString, prefix), q;
			while (p) {
				p += prefixLength;
				q = c_strstr(p, suffix);
				q += suffixLength;
				anID = c_strtol(q, &p, 10);
				if (aCount <= anID)
					aCount = anID + 1;
				p = c_strstr(p, prefix);
			}
		}
		if (aCount < the->symbolCount)
			aCount = the->symbolCount;
			
		data->symbolCount = aCount;
		data->symbolIndex = 0;
		data->symbolArray = c_calloc(aCount, sizeof(txSlot*));
		xsElseError(data->symbolArray);
		data->symbolModulo = the->symbolModulo;
		data->symbolTable = c_calloc(the->symbolModulo, sizeof(txSlot*));
		xsElseError(data->symbolTable);
	
		if (aString) {
			txString p = c_strstr(aString, prefix), q;
			while (p) {
				p += prefixLength;
				q = c_strstr(p, suffix);
				*q = 0;
				aSymbol = fxNewSymbolC(the, p);
				aModulo = aSymbol->value.symbol.sum % data->symbolModulo;
				q += suffixLength;
				anID = c_strtol(q, &p, 10);
				xscBuildSymbol(the, data, aSymbol, anID, aModulo);
				data->symbolIndex = anID + 1;
				p = c_strstr(p, prefix);
			}
			c_free(aString);
			aString = C_NULL;
		}
			
		aCount = the->symbolIndex;
		anID = 0;
		for (anIndex = 0; anIndex < aCount; anIndex++) {
			aSymbol = the->symbolArray[anIndex];
			if (aSymbol->flag & XS_TO_C_FLAG) {
				aModulo = aSymbol->value.symbol.sum % data->symbolModulo;
				result = data->symbolTable[aModulo];
				while (result != C_NULL) {
					if (result->value.symbol.sum == aSymbol->value.symbol.sum)
						#if mxCmpStr
						if (result->value.symbol.string == aSymbol->value.symbol.string)
						#endif	
							break;
					result = result->next;
				}
				if (result == C_NULL) {
					while (data->symbolArray[anID])
						anID++;
					xscBuildSymbol(the, data, aSymbol, anID, aModulo);
					if (data->symbolIndex <= anID)
						data->symbolIndex = anID + 1;
				}
			}
		}
		
		aCount = data->symbolIndex;
		anIndex = 0;
		while (anID < aCount) {
			while ((anID < aCount) && data->symbolArray[anID])
				anID++;
			if (anID < aCount) {
				char aName[16];
				sprintf(aName, "@%d", anIndex);
				aSymbol = fxNewSymbolC(the, aName);
				aModulo = aSymbol->value.symbol.sum % data->symbolModulo;
				xscBuildSymbol(the, data, aSymbol, anID, aModulo);
			}
		}
		
		aCode = aBuffer->bottom;
		xscBuildSymbolsCode(the, data, aCode);
		aCount = data->symbolIndex;
		
		aSize = 2;
		for (anIndex = 0; anIndex < aCount; anIndex++)
			aSize += strlen(data->symbolArray[anIndex]->value.symbol.string) + symbolEncodeLength + 1;
		aBuffer->symbols = c_malloc(aSize);
		xsElseError(aBuffer->symbols);
		aBuffer->symbolsSize = aSize;
		
		aCode = aBuffer->symbols;
		mxEncode2(aCode, aCount);
		for (anIndex = 0; anIndex < aCount; anIndex++) {
			aString = data->symbolArray[anIndex]->value.symbol.string;
			aLength = strlen(aString) + 1;
			memcpy(aCode, aString, aLength);
			aCode += aLength;
#if mxUseApHash
			mxEncode4(aCode,data->symbolArray[anIndex]->value.symbol.sum);
#endif
		}
		aString = C_NULL;
		failed = 0;
	}
	mxCatch(the) {
		failed = 1;
	}	
	if (data) {
		if (data->symbolTable)
			c_free(data->symbolTable);
		if (data->symbolArray) {
			aCount = data->symbolIndex;
			for (anIndex = 0; anIndex < aCount; anIndex++)
				c_free(data->symbolArray[anIndex]);
			c_free(data->symbolArray);
		}
		if (aString)
			c_free(aString);
		if (aFile)
			fclose(aFile);
		c_free(data);
	}
	if (failed)
		xsThrow(xsException);
}
Ejemplo n.º 11
0
const char *
proper_name_utf8 (const char *name_ascii, const char *name_utf8)
{
  /* See whether there is a translation.   */
  const char *translation = gettext (name_ascii);

  /* Try to convert NAME_UTF8 to the locale encoding.  */
  const char *locale_code = locale_charset ();
  char *alloc_name_converted = NULL;
  char *alloc_name_converted_translit = NULL;
  const char *name_converted = NULL;
  const char *name_converted_translit = NULL;
  const char *name;

  if (c_strcasecmp (locale_code, "UTF-8") != 0)
    {
#if HAVE_ICONV
      name_converted = alloc_name_converted =
	xstr_iconv (name_utf8, "UTF-8", locale_code);

# if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 \
     || _LIBICONV_VERSION >= 0x0105
      {
	size_t len = strlen (locale_code);
	char *locale_code_translit = (char *) xmalloc (len + 10 + 1);
	memcpy (locale_code_translit, locale_code, len);
	memcpy (locale_code_translit + len, "//TRANSLIT", 10 + 1);

	name_converted_translit = alloc_name_converted_translit =
	  xstr_iconv (name_utf8, "UTF-8", locale_code_translit);

	free (locale_code_translit);
      }
# endif
#endif
    }
  else
    {
      name_converted = name_utf8;
      name_converted_translit = name_utf8;
    }

  /* The name in locale encoding.  */
  name = (name_converted != NULL ? name_converted :
	  name_converted_translit != NULL ? name_converted_translit :
	  name_ascii);

  if (translation != name_ascii)
    {
      /* See whether the translation contains the original name.
	 A multibyte-aware strstr() is not absolutely necessary here.  */
      if (c_strstr (translation, name_ascii) != NULL
	  || (name_converted != NULL
	      && strstr (translation, name_converted) != NULL)
	  || (name_converted_translit != NULL
	      && strstr (translation, name_converted_translit) != NULL))
	{
	  if (alloc_name_converted != NULL)
	    free (alloc_name_converted);
	  if (alloc_name_converted_translit != NULL)
	    free (alloc_name_converted_translit);
	  return translation;
	}
      else
	{
	  /* Return "TRANSLATION (NAME)".  */
	  char *result =
	    (char *) xmalloc (strlen (translation) + 2 + strlen (name) + 1 + 1);

	  sprintf (result, "%s (%s)", translation, name);

	  if (alloc_name_converted != NULL)
	    free (alloc_name_converted);
	  if (alloc_name_converted_translit != NULL)
	    free (alloc_name_converted_translit);
	  return result;
	}
    }
  else
    {
      if (alloc_name_converted != NULL && alloc_name_converted != name)
	free (alloc_name_converted);
      if (alloc_name_converted_translit != NULL
	  && alloc_name_converted_translit != name)
	free (alloc_name_converted_translit);
      return name;
    }
}