示例#1
0
static int gprs_readTimeStamp( lua_State* L )
{
int timeout = luaL_checkinteger(L, 1)*1000000;
uint8_t result = 3;
char *ret;

	if( gprs_StartSocket(L) == 1 )
	{
		result = 4;
		if( gprs_SendSocket(L,"GET") == 1 )
		{
			//if( (ret = readCommand(NULL,"}\r\n","}\r\n","{\"time\":","}\r\n",L)) != NULL )
			if( (ret = readCommand(NULL,"}\r\n","}\r\n","{","}\r\n",L)) != NULL )
			{
				char saida[512];
				memset(saida,0,512);
				c_memcpy(saida,ret,c_strlen(ret));
				sendCommand((char*)CIPCLOSE, MSG_OK, MSG_NOK, strlen(CIPCLOSE), timeout/10);
				//lua_pushlstring(L,tm, 10);
				lua_pushlstring(L,saida, c_strlen(saida));
				lua_pushinteger(L,0);
				return 2;
			}else
				result = 5;
		}
	}
	lua_pushinteger(L,result);
	return 1;
}
示例#2
0
void fx_Error_toString(txMachine* the)
{
	txInteger aLength;
	
	mxPushSlot(mxThis);
	fxGetID(the, mxID(_name));
	if (the->stack->kind == XS_UNDEFINED_KIND) 
		fxCopyStringC(the, the->stack, "Error");
	else	
		fxToString(the, the->stack);
	mxPushSlot(mxThis);
	fxGetID(the, mxID(_message));
	if (the->stack->kind == XS_UNDEFINED_KIND) 
		fxCopyStringC(the, the->stack, "");
	else	
		fxToString(the, the->stack);
	aLength = c_strlen(the->stack->value.string);
	if (aLength) {
		aLength += c_strlen((the->stack + 1)->value.string) + 2;
		mxResult->value.string = (txString)fxNewChunk(the, aLength + 1);
		mxResult->kind = XS_STRING_KIND;
		c_strcpy(mxResult->value.string, (the->stack + 1)->value.string);
		c_strcat(mxResult->value.string, ": ");
		c_strcat(mxResult->value.string, the->stack->value.string);
		the->stack++;
		the->stack++;
	}
	else {
		the->stack++;
		mxPullSlot(mxResult);
	}
}
示例#3
0
char            *c_getnextline(const int fd, char c)
{
  int		 i;
  char		buff;
  char		*ptr;

  ptr = c_malloc(sizeof(char) * 1);
  ptr[0] = '\0';
  i = 0;
  if (read(fd, &buff, 1) != 1)
    return (NULL);
  if (buff <= 31 || buff > 176)
    c_puterror("fichier corrompu");
  ptr = c_realloc(ptr, sizeof(char) * (c_strlen(ptr, '\0') + 2));
  ptr[i++] = buff;
  ptr[i] = '\0';
  while (read(fd, &buff, 1) == 1 && buff != '\n' && buff != c)
    {
      if (buff <= 31 || buff > 176)
	c_puterror("fichier corrompu");
      ptr = realloc(ptr, sizeof(char) * (c_strlen(ptr, '\0') + 2));
      ptr[i++] = buff;
      ptr[i] = '\0';
    }
  return (ptr);
}
/*
** add length modifier into formats
*/
static void addlenmod (char *form, const char *lenmod) {
  size_t l = c_strlen(form);
  size_t lm = c_strlen(lenmod);
  char spec = form[l - 1];
  c_strcpy(form + l - 1, lenmod);
  form[l + lm - 1] = spec;
  form[l + lm] = '\0';
}
示例#5
0
void fxBufferFunctionName(txMachine* the, txString buffer, txSize size, txSlot* function, txString suffix)
{
	txSlot* slot = fxGetProperty(the, function, mxID(_name));
	if (slot && ((slot->kind == XS_STRING_KIND) || (slot->kind == XS_STRING_X_KIND))) {
		c_strncat(buffer, slot->value.string, size - c_strlen(buffer) - 1);
		c_strncat(buffer, suffix, size - c_strlen(buffer) - 1);
	}
}
示例#6
0
void fxBufferObjectName(txMachine* the, txString buffer, txSize size, txSlot* object, txString suffix)
{
	txSlot* slot = fxGetProperty(the, object, mxID(_Symbol_toStringTag));
	if (slot && ((slot->kind == XS_STRING_KIND) || (slot->kind == XS_STRING_X_KIND))) {
		c_strncat(buffer, slot->value.string, size - c_strlen(buffer) - 1);
		c_strncat(buffer, suffix, size - c_strlen(buffer) - 1);
	}
}
示例#7
0
/**
  * @brief  Wifi ap scan over callback to display.
  * @param  arg: contain the aps information
  * @param  status: scan over status
  * @retval None
  */
static void wifi_scan_done(void *arg, STATUS status)
{
  uint8 ssid[33];
  char temp[128];
  if(wifi_scan_succeed == LUA_NOREF)
    return;
  if(arg == NULL)
    return;
  
  lua_rawgeti(gL, LUA_REGISTRYINDEX, wifi_scan_succeed);

  if (status == OK)
  {
    struct bss_info *bss_link = (struct bss_info *)arg;
    bss_link = bss_link->next.stqe_next;//ignore first
    lua_newtable( gL );

    while (bss_link != NULL)
    {
      c_memset(ssid, 0, 33);
      if (c_strlen(bss_link->ssid) <= 32)
      {
        c_memcpy(ssid, bss_link->ssid, c_strlen(bss_link->ssid));
      }
      else
      {
        c_memcpy(ssid, bss_link->ssid, 32);
      }
      if(getap_output_format==1) //use new format(BSSID : SSID, RSSI, Authmode, Channel)
      {
    	c_sprintf(temp,"%s,%d,%d,%d", ssid, bss_link->rssi, bss_link->authmode, bss_link->channel);
    	lua_pushstring(gL, temp);
        NODE_DBG(MACSTR" : %s\n",MAC2STR(bss_link->bssid) , temp);
    	c_sprintf(temp,MACSTR, MAC2STR(bss_link->bssid));
    	lua_setfield( gL, -2,  temp);
      }
      else//use old format(SSID : Authmode, RSSI, BSSID, Channel)
      {
  	    c_sprintf(temp,"%d,%d,"MACSTR",%d", bss_link->authmode, bss_link->rssi, MAC2STR(bss_link->bssid),bss_link->channel);
        lua_pushstring(gL, temp);
        lua_setfield( gL, -2, ssid );
        NODE_DBG("%s : %s\n", ssid, temp);
      }

      bss_link = bss_link->next.stqe_next;
    }
  }
  else
  {
    lua_newtable( gL );
  }
  lua_call(gL, 1, 0);
  if(wifi_scan_succeed != LUA_NOREF)
  {
    luaL_unref(gL, LUA_REGISTRYINDEX, wifi_scan_succeed);
    wifi_scan_succeed = LUA_NOREF;
  }
}
示例#8
0
// Lua: compile(filename) -- compile lua file into lua bytecode, and save to .lc
static int node_compile( lua_State* L )
{
  Proto* f;
  int file_fd = FS_OPEN_OK - 1;
  size_t len;
  const char *fname = luaL_checklstring( L, 1, &len );
  if ( len > FS_NAME_MAX_LENGTH )
    return luaL_error(L, "filename too long");

  char output[FS_NAME_MAX_LENGTH];
  c_strcpy(output, fname);
  // check here that filename end with ".lua".
  if (len < 4 || (c_strcmp( output + len - 4, ".lua") != 0) )
    return luaL_error(L, "not a .lua file");

  output[c_strlen(output) - 2] = 'c';
  output[c_strlen(output) - 1] = '\0';
  NODE_DBG(output);
  NODE_DBG("\n");
  if (luaL_loadfsfile(L, fname) != 0) {
    return luaL_error(L, lua_tostring(L, -1));
  }

  f = toproto(L, -1);

  int stripping = 1;      /* strip debug information? */

  file_fd = fs_open(output, fs_mode2flag("w+"));
  if (file_fd < FS_OPEN_OK)
  {
    return luaL_error(L, "cannot open/write to file");
  }

  lua_lock(L);
  int result = luaU_dump(L, f, writer, &file_fd, stripping);
  lua_unlock(L);

  if (fs_flush(file_fd) < 0) {   // result codes aren't propagated by flash_fs.h
    // overwrite Lua error, like writer() does in case of a file io error
    result = 1;
  }
  fs_close(file_fd);
  file_fd = FS_OPEN_OK - 1;

  if (result == LUA_ERR_CC_INTOVERFLOW) {
    return luaL_error(L, "value too big or small for target integer type");
  }
  if (result == LUA_ERR_CC_NOTINTEGER) {
    return luaL_error(L, "target lua_Number is integral but fractional value found");
  }
  if (result == 1) {    // result status generated by writer() or fs_flush() fail
    return luaL_error(L, "writing to file failed");
  }

  return 0;
}
示例#9
0
文件: endpoints.c 项目: pvvx/EspLua
static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
    const coap_option_t *opt;
    uint8_t count;
    int n;
    if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count)))
    {
        if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable]
        {
            NODE_DBG("should never happen.\n");
            goto end;
        }
        if (count == ep->path->count + 1)
        {
            coap_luser_entry *h = ep->user_entry->next;     // ->next: skip the first entry(head)
            while(NULL != h){
                if (opt[count-1].buf.len != c_strlen(h->name))
                {
                    h = h->next;
                    continue;
                }
                if (0 == c_memcmp(h->name, opt[count-1].buf.p, opt[count-1].buf.len))
                {
                    NODE_DBG("/v1/v/");
                    NODE_DBG_((char *)h->name);
                    NODE_DBG(" match.\n");
                    if(h->L == NULL)
                        return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
                    if(c_strlen(h->name))
                    {
                        n = lua_gettop(h->L);
                        lua_getglobal(h->L, h->name);
                        if (!lua_isnumber(h->L, -1)) {
                            NODE_DBG ("should be a number.\n");
                            lua_settop(h->L, n);
                            return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
                        } else {
                            const char *res = lua_tostring(h->L,-1);
                            lua_settop(h->L, n);
                            return coap_make_response(scratch, outpkt, (const uint8_t *)res, c_strlen(res), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
                        }
                    }
                } else {
                    h = h->next;
                }
            }
        }else{
            NODE_DBG("/v1/v match.\n");
            goto end;
        }
    }
    NODE_DBG("none match.\n");
end:
    return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
}
示例#10
0
txString fxConcatString(txMachine* the, txSlot* a, txSlot* b)
{
	txSize aSize = c_strlen(a->value.string);
	txSize bSize = c_strlen(b->value.string);
	txString result = (txString)fxNewChunk(the, aSize + bSize + 1);
	c_memcpy(result, a->value.string, aSize);
	c_memcpy(result + aSize, b->value.string, bSize + 1);
	a->value.string = result;
	a->kind = XS_STRING_KIND;
	return result;
}
示例#11
0
sint32_t vfs_chdir( const char *path )
{
    vfs_fs_fns *fs_fns;
    const char *normpath = normalize_path( path );
    const char *level;
    char *outname;
    int ok = VFS_RES_ERR;

#if LDRV_TRAVERSAL
    // track dir level
    if (normpath[0] == '/') {
        dir_level = 0;
        level = &(normpath[1]);
    } else {
        level = normpath;
    }
    while (c_strlen( level ) > 0) {
        dir_level++;
        if (level = c_strchr( level, '/' )) {
            level++;
        } else {
            break;
        }
    }
#endif

#ifdef BUILD_SPIFFS
    if (fs_fns = myspiffs_realm( normpath, &outname, TRUE )) {
        // our SPIFFS integration doesn't support directories
        if (c_strlen( outname ) == 0) {
            ok = VFS_RES_OK;
        }
    }
#endif

#ifdef BUILD_FATFS
    if (fs_fns = myfatfs_realm( normpath, &outname, TRUE )) {
        if (c_strchr( outname, ':' )) {
            // need to set FatFS' default drive
            fs_fns->chdrive( outname );
            // and force chdir to root in case path points only to root
            fs_fns->chdir( "/" );
        }
        if (fs_fns->chdir( outname ) == VFS_RES_OK) {
            ok = VFS_RES_OK;
        }
        c_free( outname );
    }
#endif

    return ok == VFS_RES_OK ? VFS_RES_OK : VFS_RES_ERR;
}
示例#12
0
txModuleData* fxLoadLibrary(txMachine* the, txString path, txScript* script)
{
	txModuleData* data = C_NULL;
	if (script) {
		if (script->version[3] == 1) {
			char buffer[PATH_MAX];
			char* dot;
			void* library;
			txCallback callback;
			c_strcpy(buffer, path);
			dot = c_strrchr(buffer, '.');
			if (dot)
				*dot = 0;
			#if mxWindows
				c_strcat(buffer, ".dll");
				#if mxReport
					fxReport(the, "# Loading library \"%s\"\n", buffer);
				#endif
				library = LoadLibrary(buffer);
				mxElseError(library);
				callback = (txCallback)GetProcAddress(library, "xsHostModule");
				mxElseError(callback);
			#else
				c_strcat(buffer, ".so");
				#if mxReport
					fxReport(the, "# Loading library \"%s\"\n", buffer);
				#endif
				library = dlopen(buffer, RTLD_NOW);
				mxElseError(library);
				callback = (txCallback)dlsym(library, "xsHostModule");
				mxElseError(callback);
			#endif
			data = c_malloc(sizeof(txModuleData) + c_strlen(buffer));
			mxElseError(data);
			data->library = library;
			data->the = the;
			c_strcpy(data->path, buffer);
			script->callback = callback;
			return data;
		}
	}
#if mxReport
	data = c_malloc(sizeof(txModuleData) + c_strlen(path));
	mxElseError(data);
	data->library = C_NULL;
	data->the = the;
	c_strcpy(data->path, path);
	fxReport(the, "# Loading module \"%s\"\n", path);
#endif
	return data;
}
示例#13
0
void fxThrowMessage(txMachine* the, txString path, txInteger line, txError error, txString format, ...)
{
	char message[128] = "";
	txInteger length = 0;
    va_list arguments;
    txSlot* slot;
	fxBufferFrameName(the, message, sizeof(message), the->frame, ": ");
	length = c_strlen(message);
    va_start(arguments, format);
    vsnprintf(message + length, sizeof(message) - length, format, arguments);
    va_end(arguments);
	if ((error <= XS_NO_ERROR) || (XS_ERROR_COUNT <= error))
		error = XS_UNKNOWN_ERROR;
    
    slot = fxNewSlot(the);
    slot->kind = XS_INSTANCE_KIND;
    slot->value.instance.garbage = C_NULL;
    slot->value.instance.prototype = mxErrorPrototypes(error).value.reference;
	mxException.kind = XS_REFERENCE_KIND;
	mxException.value.reference = slot;
	slot = fxNextStringProperty(the, slot, message, mxID(_message), XS_DONT_ENUM_FLAG);
#ifdef mxDebug
	fxDebugThrow(the, path, line, message);
#endif
	fxJump(the);
}
示例#14
0
LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
                                       const char *fname, int szhint) {
  const char *e;
  lua_pushvalue(L, idx);
  do {
    e = c_strchr(fname, '.');
    if (e == NULL) e = fname + c_strlen(fname);
    lua_pushlstring(L, fname, e - fname);
    lua_rawget(L, -2);
    if (lua_isnil(L, -1)) {
      /* If looking for a global variable, check the rotables too */
      void *ptable = luaR_findglobal(fname, e - fname);
      if (ptable) {
        lua_pop(L, 1);
        lua_pushrotable(L, ptable);
      }
    }
    if (lua_isnil(L, -1)) {  /* no such field? */
      lua_pop(L, 1);  /* remove this nil */
      lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
      lua_pushlstring(L, fname, e - fname);
      lua_pushvalue(L, -2);
      lua_settable(L, -4);  /* set new table into field */
    }
    else if (!lua_istable(L, -1) && !lua_isrotable(L, -1)) {  /* field has a non-table value? */
      lua_pop(L, 2);  /* remove table and value */
      return fname;  /* return problematic part of the name */
    }
    lua_remove(L, -2);  /* remove previous table */
    fname = e + 1;
  } while (*e == '.');
  return NULL;
}
示例#15
0
static void
cipher_process(xsMachine *the, kcl_symmetric_direction_t direction)
{
	crypt_cipher_t *cipher = xsGetHostData(xsThis);
	size_t len;
	void *indata, *outdata;

	if (cipher->keysched != NULL) {
		if (cipher->direction != direction) {
			(*cipher->keysched)(cipher->ctx, direction);
			cipher->direction = direction;
		}
	}
	if (xsToInteger(xsArgc) > 1 && xsTest(xsArg(1))) {
		if (xsGetArrayBufferLength(xsArg(1)) < (xsIntegerValue)cipher->blockSize)
			crypt_throw_error(the, "cipher: too small buffer");
		xsResult = xsArg(1);
	}
	else
		xsResult = xsArrayBuffer(NULL, cipher->blockSize);
	if (xsTypeOf(xsArg(0)) == xsStringType) {
		indata = xsToString(xsArg(0));
		len = c_strlen(indata);
	}
	else {
		indata = xsToArrayBuffer(xsArg(0));
		len = xsGetArrayBufferLength(xsArg(0));
	}
	if (len < cipher->blockSize)
		crypt_throw_error(the, "cipher: wrong size");
	outdata = xsToArrayBuffer(xsResult);
	(*cipher->process)(cipher->ctx, indata, outdata);
}
示例#16
0
void fxLoadModuleXML(txMachine* the, txString path, txID moduleID)
{
	FskFileMapping map = NULL;
	txFileMapStream fileMapStream;
	txStringStream stringStream;
	txScript* script = NULL;
	
	mxTry(the) {
		fxBeginHost(the);
		xsThrowIfFskErr(FskFileMap(path, &fileMapStream.buffer, &fileMapStream.size, 0, &map));
		fileMapStream.offset = 0;
		mxPushInteger(0);
		mxPushInteger(0);
		mxPushInteger(0);
		mxPushInteger(0);
		mxPushInteger(3);
		fxParse(the, &fileMapStream, fxFileMapGetter, path, 1, xsSourceFlag | xsDebugFlag);
		fxCallID(the, fxID(the, "generate"));
		fxToString(the, the->stack);
		stringStream.slot = the->stack;
		stringStream.size = c_strlen(stringStream.slot->value.string);
		stringStream.offset = 0;
		script = fxParseScript(the, &stringStream, fxStringGetter, mxDebugFlag);
		fxEndHost(the);
	}
	mxCatch(the) {
		break;
	}
	FskFileDisposeMap(map);
	fxResolveModule(the, moduleID, script, NULL, NULL);
}
示例#17
0
void
xs_stream_encrypt(xsMachine *the)
{
	crypt_stream_t *stream = xsGetHostData(xsThis);
	int ac = xsToInteger(xsArgc);
	size_t len;
	void *indata, *outdata;

	if (xsTypeOf(xsArg(0)) == xsStringType)
		len = c_strlen(xsToString(xsArg(0)));
	else
		len = xsGetArrayBufferLength(xsArg(0));
	if (ac > 2 && xsTypeOf(xsArg(2)) != xsUndefinedType) {
		size_t n = xsToInteger(xsArg(2));
		if (n < len)
			len = n;
	}
	if (ac > 1 && xsTest(xsArg(1))) {
		if (xsGetArrayBufferLength(xsArg(1)) < (xsIntegerValue)len)
			crypt_throw_error(the, "too small buffer");
		xsResult = xsArg(1);
	}
	else
		xsResult = xsArrayBuffer(NULL, len);
	if (xsTypeOf(xsArg(0)) == xsStringType)
		indata = xsToString(xsArg(0));
	else
		indata = xsToArrayBuffer(xsArg(0));
	outdata = xsToArrayBuffer(xsResult);
	(*stream->process)(stream->ctx, indata, outdata, len);
}
示例#18
0
void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  if (*source == '=') {
    c_strncpy(out, source+1, bufflen);  /* remove first char */
    out[bufflen-1] = '\0';  /* ensures null termination */
  }
  else {  /* out = "source", or "...source" */
    if (*source == '@') {
      size_t l;
      source++;  /* skip the `@' */
      bufflen -= sizeof(" '...' ");
      l = c_strlen(source);
      c_strcpy(out, "");
      if (l > bufflen) {
        source += (l-bufflen);  /* get last part of file name */
        c_strcat(out, "...");
      }
      c_strcat(out, source);
    }
    else {  /* out = [string "string"] */
      size_t len = c_strcspn(source, "\n\r");  /* stop at first newline */
      bufflen -= sizeof(" [string \"...\"] ");
      if (len > bufflen) len = bufflen;
      c_strcpy(out, "[string \"");
      if (source[len] != '\0') {  /* must truncate? */
        c_strncat(out, source, len);
        c_strcat(out, "...");
      }
      else
        c_strcat(out, source);
      c_strcat(out, "\"]");
    }
  }
}
示例#19
0
static void addintlen (char *form) {
  size_t l = c_strlen(form);
  char spec = form[l - 1];
  c_strcpy(form + l - 1, LUA_INTFRMLEN);
  form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
  form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
}
示例#20
0
void fx_JSON_parse(txMachine* the)
{
	txSlot* aSlot;
	txStringCStream aCStream;
	txStringStream aStream;

	aSlot = fxGetInstance(the, mxThis);
	if (aSlot->flag & XS_SANDBOX_FLAG)
		the->frame->flag |= XS_SANDBOX_FLAG;
	else
		the->frame->flag |= the->frame->next->flag & XS_SANDBOX_FLAG;
	if (mxArgc < 1)
		mxDebug0(the, XS_SYNTAX_ERROR, "JSON.parse: no buffer");
	aSlot = fxGetInstance(the, mxArgv(0));
	if (mxIsChunk(aSlot)) {
		aSlot = aSlot->next;
		aCStream.buffer = aSlot->value.host.data;
		aCStream.size = aSlot->next->value.integer;
		aCStream.offset = 0;
		fxParseJSON(the, &aCStream, fxStringCGetter);
	}
	else {
		fxToString(the, mxArgv(0));
		aStream.slot = mxArgv(0);
		aStream.size = c_strlen(aStream.slot->value.string);
		aStream.offset = 0;
		fxParseJSON(the, &aStream, fxStringGetter);
	}
	*mxResult = *(the->stack++);
}
示例#21
0
文件: xsl6.c 项目: kouis3940/kinomajs
void fxWriteScriptHosts(txLinkerScript* script, FILE* file)
{
	txByte* p = script->hostsBuffer;
	if (p) {
		txID c, i, id;
		mxDecode2(p, c);
		fprintf(file, "void xsHostModule%d(xsMachine* the)\n", script->scriptIndex);
		fprintf(file, "{\n");
		fprintf(file, "\tstatic const xsHostBuilder builders[%d] = {\n", c);
		for (i = 0; i < c; i++) {
			txS1 length = *p++;
			mxDecode2(p, id);
			if (id != XS_NO_ID)
				id &= 0x7FFF;
			if (length < 0)
				fprintf(file, "\t\t{ (xsCallback)%s, -1, -1 },\n", p);
			else
				fprintf(file, "\t\t{ %s, %d, %d },\n", p, length, id);
			p += c_strlen((char*)p) + 1;
		}
		fprintf(file, "\t};\n");
		fprintf(file, "\txsResult = xsBuildHosts(%d, (xsHostBuilder*)builders);\n", c);
		fprintf(file, "}\n\n");
	}
}
示例#22
0
/*
** If your system does not support `stdout', you can just remove this function.
** If you need, you can define your own `print' function, following this
** model but changing `fputs' to put the strings at a proper place
** (a console window or a log file, for instance).
*/
static int luaB_print (lua_State *L) {
  int n = lua_gettop(L);  /* number of arguments */
  int i;
  lua_getglobal(L, "tostring");
  for (i=1; i<=n; i++) {
    const char *s;
    lua_pushvalue(L, -1);  /* function to be called */
    lua_pushvalue(L, i);   /* value to print */
    lua_call(L, 1, 1);
    s = lua_tostring(L, -1);  /* get result */
    if (s == NULL)
      return luaL_error(L, LUA_QL("tostring") " must return a string to "
                           LUA_QL("print"));
#if defined(LUA_USE_STDIO)
    if (i>1) c_fputs("\t", c_stdout);
    c_fputs(s, c_stdout);
#else
    if (i>1)  luai_writestring("\t", 1);
    luai_writestring(s, c_strlen(s));
#endif
    lua_pop(L, 1);  /* pop result */
  }
#if defined(LUA_USE_STDIO)
  c_fputs("\n", c_stdout);
#else
  luai_writeline();
#endif
  return 0;
}
示例#23
0
文件: endpoints.c 项目: pvvx/EspLua
static int handle_post_command(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
    if (inpkt->payload.len == 0)
        return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
    if (inpkt->payload.len > 0)
    {
        lua_Load *load = &gLoad;
        if(load->line_position == 0){
            coap_buffer_to_string(load->line, load->len,&inpkt->payload);
            load->line_position = c_strlen(load->line)+1;
            // load->line[load->line_position-1] = '\n';
            // load->line[load->line_position] = 0;
            // load->line_position++;
            load->done = 1;
            NODE_DBG("Get command:\n");
            NODE_DBG_(load->line); // buggy here
            NODE_DBG("\nResult(if any):\n");
/*            os_timer_disarm(&lua_timer);
            os_timer_setfn(&lua_timer, (os_timer_func_t *)dojob, load);
            os_timer_arm(&lua_timer, READLINE_INTERVAL, 0);   // no repeat */
			set_lua_dojob(load);
        }
        return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
    }
}
示例#24
0
static const char *normalize_path( const char *path )
{
#if ! LDRV_TRAVERSAL
    return path;
#else
    const char *temp = path;
    size_t len;

    while ((len = c_strlen( temp )) >= 2) {
        if (temp[0] == '.' && temp[1] == '.') {
            --dir_level;
            if (len >= 4 && dir_level > 0) {
                // prepare next step
                temp = &(temp[4]);
            } else {
                // we're at top, the remainder is expected be an absolute path
                temp = &(temp[3]);
            }
        } else {
            break;
        }
    }

    if (dir_level > 0) {
        // no traversal on root level
        return path;
    } else {
        // path traverses via root
        return temp;
    }
#endif
}
示例#25
0
mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id)
{
  init_message(connection);

  if(topic == NULL || topic[0] == '\0')
    return fail_message(connection);

  if(append_string(connection, topic, c_strlen(topic)) < 0)
    return fail_message(connection);

  if(qos > 0)
  {
    if((*message_id = append_message_id(connection, 0)) == 0)
      return fail_message(connection);
  }
  else
    *message_id = 0;

  if(connection->message.length + data_length > connection->buffer_length)
    return fail_message(connection);
  c_memcpy(connection->buffer + connection->message.length, data, data_length);
  connection->message.length += data_length;

  return fini_message(connection, MQTT_MSG_TYPE_PUBLISH, 0, qos, retain);
}
示例#26
0
txString fxMergePath(txString base, txString name, txString path)
{
	txSize baseLength, nameLength;
	txString separator = strrchr(base, mxSeparator);
	if (separator) {
		separator++;
		baseLength = separator - base;
	}
	else
		baseLength = 0;
	nameLength = c_strlen(name);
	if (baseLength)
		c_memcpy(path, base, baseLength);
#if mxWindows
	{
		char c ;
		separator = path + baseLength;
		while ((c = *name)) {
			if (c == '/')
				*separator++ = mxSeparator;
			else
				*separator++ = c;
			name++;
		}
		*separator = 0;
	}
#else		
	c_memcpy(path + baseLength, name, nameLength + 1);
#endif
	return path;
}
示例#27
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;
}
示例#28
0
txString fxConcatStringC(txMachine* the, txSlot* a, txString b)
{
	txSize aSize = c_strlen(a->value.string);
	txSize bSize = c_strlen(b);
	txSize resultSize = aSize + bSize + 1;
	txString result = C_NULL;
	if (a->kind == XS_STRING_KIND)
		result = (txString)fxRenewChunk(the, a->value.string, resultSize);
	if (!result) {
		result = (txString)fxNewChunk(the, resultSize);
		c_memcpy(result, a->value.string, aSize);
		a->value.string = result;
		a->kind = XS_STRING_KIND;
	}
	c_memcpy(result + aSize, b, bSize + 1);
	return result;
}
示例#29
0
/**
  * @brief  Wifi ap scan over callback to display.
  * @param  arg: contain the aps information
  * @param  status: scan over status
  * @retval None
  */
static void wifi_scan_done(void *arg, STATUS status)
{
  uint8 ssid[33];
  char temp[128];
  if(wifi_scan_succeed == LUA_NOREF)
    return;
  if(arg == NULL)
    return;
  
  lua_rawgeti(gL, LUA_REGISTRYINDEX, wifi_scan_succeed);

  if (status == OK)
  {
    struct bss_info *bss_link = (struct bss_info *)arg;
    bss_link = bss_link->next.stqe_next;//ignore first
    lua_newtable( gL );

    while (bss_link != NULL)
    {
      c_memset(ssid, 0, 33);
      if (c_strlen(bss_link->ssid) <= 32)
      {
        c_memcpy(ssid, bss_link->ssid, c_strlen(bss_link->ssid));
      }
      else
      {
        c_memcpy(ssid, bss_link->ssid, 32);
      }
      c_sprintf(temp,"%d,%d,"MACSTR",%d", bss_link->authmode, bss_link->rssi,
                 MAC2STR(bss_link->bssid),bss_link->channel);

      lua_pushstring(gL, temp);
      lua_setfield( gL, -2, ssid );

      // NODE_DBG(temp);

      bss_link = bss_link->next.stqe_next;
    }
  }
  else
  {
    lua_pushnil(gL);
  }
  lua_call(gL, 1, 0);
}
示例#30
0
LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
                                        const char *def, size_t *len) {
  if (lua_isnoneornil(L, narg)) {
    if (len)
      *len = (def ? c_strlen(def) : 0);
    return def;
  }
  else return luaL_checklstring(L, narg, len);
}