Exemplo n.º 1
0
int my_lua_write_field(lua_State *state)
{
    int n = lua_gettop(state);
    if(n != 0 && n!= 1)
        luaL_error(state, "write() takes one or no argument");
    soc_addr_t addr = lua_tounsigned(state, lua_upvalueindex(1));
    soc_word_t shift = lua_tounsigned(state, lua_upvalueindex(2));
    soc_word_t mask = lua_tounsigned(state, lua_upvalueindex(3));
    bool is_sct = lua_toboolean(state, lua_upvalueindex(5));

    soc_word_t value = mask;
    if(n == 1)
    {
        if(!lua_isnumber(state, 1) && lua_isstring(state, 1))
        {
            lua_pushvalue(state, lua_upvalueindex(4));
            lua_pushvalue(state, 1);
            lua_gettable(state, -2);
            if(lua_isnil(state, -1))
                luaL_error(state, "field has no value %s", lua_tostring(state, 1));
            value = luaL_checkunsigned(state, -1);
            lua_pop(state, 2);
        }
        else
            value = luaL_checkunsigned(state, 1);
        value &= mask;
    }

    if(!is_sct)
        value = value << shift | (hw_read32(state, addr) & ~(mask << shift));
    else
        value <<= shift;

    hw_write32(state, addr, value);
    return 0;
}
Exemplo n.º 2
0
LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) {
  const lua_Number *v = lua_version(L);
  if (v != lua_version(NULL))
	luaL_error(L, "multiple Lua VMs detected");
  else if (*v != ver)
	luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
				  ver, *v);
  /* check conversions number -> integer types */
  lua_pushnumber(L, -(lua_Number)0x1234);
  if (lua_tointeger(L, -1) != -0x1234 ||
	  lua_tounsigned(L, -1) != (lua_Unsigned)-0x1234)
	luaL_error(L, "bad conversion number->int;"
				  " must recompile Lua with proper settings");
  lua_pop(L, 1);
}
Exemplo n.º 3
0
/*
	int texture
	table float[16]  
	uint32_t color
	uint32_t additive
 */
static int
ldraw(lua_State *L) {
	int tex = (int)luaL_checkinteger(L,1);
	int texid = texture_glid(tex);
	if (texid == 0) {
		lua_pushboolean(L,0);
		return 1;
	} 
	luaL_checktype(L, 2, LUA_TTABLE);
	uint32_t color = 0xffffffff;

	if (!lua_isnoneornil(L,3)) {
		color = (uint32_t)lua_tounsigned(L,3);
	}
	uint32_t additive = (uint32_t)luaL_optunsigned(L,4,0);
	shader_program(PROGRAM_PICTURE,additive);
	shader_texture(texid);
	int n = lua_rawlen(L, 2);
	int point = n/4;
	if (point * 4 != n) {
		return luaL_error(L, "Invalid polygon");
	}
	ARRAY(float, vb, n);
	int i;
	for (i=0;i<point;i++) {
		lua_rawgeti(L, 2, i*2+1);
		lua_rawgeti(L, 2, i*2+2);
		lua_rawgeti(L, 2, point*2+i*2+1);
		lua_rawgeti(L, 2, point*2+i*2+2);
		float tx = lua_tonumber(L, -4);
		float ty = lua_tonumber(L, -3);
		float vx = lua_tonumber(L, -2);
		float vy = lua_tonumber(L, -1);
		lua_pop(L,4);
		screen_trans(&vx,&vy);
		texture_coord(tex, &tx, &ty);
		vb[i*4+0] = vx + 1.0f;
		vb[i*4+1] = vy - 1.0f;
		vb[i*4+2] = tx;
		vb[i*4+3] = ty;
	}
	if (point == 4) {
		shader_draw(vb, color);
	} else {
		shader_drawpolygon(point, vb, color);
	}
	return 0;
}
Exemplo n.º 4
0
void luaL_checkversion_ (LuaThread *L, double ver) {
  THREAD_CHECK(L);
  const double *v = lua_version(L);
  if (v != lua_version(NULL))
    luaL_error(L, "multiple Lua VMs detected");
  else if (*v != ver)
    luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
                  ver, *v);
  /* check conversions number -> integer types */
  lua_pushnumber(L, -(double)0x1234);
  if (lua_tointeger(L, -1) != -0x1234 ||
      lua_tounsigned(L, -1) != (uint32_t)-0x1234)
    luaL_error(L, "bad conversion number->int;"
                  " must recompile Lua with proper settings");
  L->stack_.pop();
}
Exemplo n.º 5
0
	uint32_t Event_HUD( void ) {
		uint32_t events = 0u;

#ifdef JPLUA
		plugin_t *plugin = NULL;
		while ( IteratePlugins( &plugin ) ) {
			if ( plugin->eventListeners[JPLUA_EVENT_HUD] ) {
				lua_rawgeti( ls.L, LUA_REGISTRYINDEX, plugin->eventListeners[JPLUA_EVENT_HUD] );
				lua_pushunsigned( ls.L, events );
				Call( ls.L, 1, 1 );
				events |= lua_tounsigned( ls.L, -1 );
			}
		}
#endif //JPLUA

		return events;
	}
Exemplo n.º 6
0
static int GetRegsByProfile(lua_State * L) {
	if(lua_gettop(L) != 1) {
        luaL_error(L, "bad argument count to 'GetRegsByProfile' (1 expected, got %d)", lua_gettop(L));
        lua_settop(L, 0);
        lua_pushnil(L);
        return 1;
    }

    if(lua_type(L, 1) != LUA_TNUMBER) {
        luaL_checktype(L, 1, LUA_TNUMBER);
		lua_settop(L, 0);
		lua_pushnil(L);
        return 1;
    }

#if LUA_VERSION_NUM < 503
	uint16_t iProfile = (uint16_t)lua_tonumber(L, 1);
#else
    uint16_t iProfile = (uint16_t)lua_tounsigned(L, 1);
#endif

    lua_settop(L, 0);

    lua_newtable(L);
    int t = lua_gettop(L), i = 0;

	RegUser *next = clsRegManager::mPtr->RegListS;
        
    while(next != NULL) {
        RegUser *cur = next;
        next = cur->next;
        
		if(cur->ui16Profile == iProfile) {
#if LUA_VERSION_NUM < 503
			lua_pushnumber(L, ++i);
#else
            lua_pushunsigned(L, ++i);
#endif
            PushReg(L, cur);
            lua_rawset(L, t);
        }
    }
    
    return 1;
}
Exemplo n.º 7
0
static int lua_mmio_read8(lua_State *L) {
    mmio_t *mmio;
    uint8_t value;
    uintptr_t offset;
    int ret;

    mmio = luaL_checkudata(L, 1, "periphery.MMIO");
    lua_mmio_checktype(L, 2, LUA_TNUMBER);

    offset = lua_tounsigned(L, 2);

    if ((ret = mmio_read8(mmio, offset, &value)) < 0)
        return lua_mmio_error(L, ret, mmio_errno(mmio), "Error: %s", mmio_errmsg(mmio));

    lua_pushunsigned(L, value);

    return 1;
}
Exemplo n.º 8
0
static int lua_serial_poll(lua_State *L) {
    serial_t *serial;
    int timeout_ms;
    int ret;

    serial = luaL_checkudata(L, 1, "periphery.Serial");

    lua_serial_checktype(L, 2, LUA_TNUMBER);

    timeout_ms = lua_tounsigned(L, 2);

    if ((ret = serial_poll(serial, timeout_ms)) < 0)
        return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

    lua_pushboolean(L, ret);

    return 1;
}
Exemplo n.º 9
0
int Srt_Md5(lua_State* ls)
{
	const char* szBuff = NULL;
	unsigned int nBuffSize = 0;

	if (lua_isstring(ls,1)){
		szBuff = lua_tostring(ls,1);
	}
	nBuffSize = strlen(szBuff);

	if (lua_isnumber(ls,2)){
		nBuffSize = lua_tounsigned(ls,2);
	}

	CKpMd5 oMd5(szBuff,nBuffSize);
	lua_pushfstring(ls,"%s",oMd5.toString().c_str());
	return 1;
}
Exemplo n.º 10
0
static int lua_send_privmsg(lua_State *L)
{
	void *session;
	const char *origin,*mynick,*msg;
	int type;
	int result=LIBIRC_ERR_INVAL;
	if(lua_gettop(L)==5){
		//(void *session,char *origin,char *mynick,char *msg,int type)
		session=lua_touserdata(L,1);
		origin=lua_tostring(L,2);
		mynick=lua_tostring(L,3);
		msg=lua_tostring(L,4);
		type=lua_tounsigned(L,5);
		if(session && origin && mynick && msg)
			result=privmsg_event(session,origin,mynick,msg,type);
	}
	lua_pushinteger(L,result);
	return 1;
}
Exemplo n.º 11
0
static int lua_likwid_memSweep(lua_State* L)
{
    int i;
    int nrThreads = luaL_checknumber(L,1);
    luaL_argcheck(L, nrThreads > 0, 1, "Thread count must be greater than 0");
    int cpus[nrThreads];
    if (!lua_istable(L, -1)) {
      lua_pushstring(L,"No table given as second argument");
      lua_error(L);
    }
    for (i = 1; i <= nrThreads; i++)
    {
        lua_rawgeti(L,-1,i);
        cpus[i-1] = lua_tounsigned(L,-1);
        lua_pop(L,1);
    }
    memsweep_threadGroup(cpus, nrThreads);
    return 0;
}
Exemplo n.º 12
0
static int lua_likwid_setMemInterleaved(lua_State* L)
{
    int ret;
    int nrThreads = luaL_checknumber(L,1);
    luaL_argcheck(L, nrThreads > 0, 1, "Thread count must be greater than 0");
    int cpus[nrThreads];
    if (!lua_istable(L, -1)) {
      lua_pushstring(L,"No table given as second argument");
      lua_error(L);
    }
    for (ret = 1; ret<=nrThreads; ret++)
    {
        lua_rawgeti(L,-1,ret);
        cpus[ret-1] = lua_tounsigned(L,-1);
        lua_pop(L,1);
    }
    numa_setInterleaved(cpus, nrThreads);
    return 0;
}
Exemplo n.º 13
0
static int lua_spi_transfer(lua_State *L) {
    spi_t *spi;
    uint8_t *buf;
    unsigned int i, len;
    int ret;

    spi = luaL_checkudata(L, 1, "periphery.SPI");
    lua_spi_checktype(L, 2, LUA_TTABLE);

    len = luaL_len(L, 2);

    if ((buf = malloc(len)) == NULL)
        return lua_spi_error(L, SPI_ERROR_ALLOC, errno, "Error: allocating memory");

    /* Convert byte table to byte buffer */
    for (i = 0; i < len; i++) {
        lua_pushunsigned(L, i+1);
        lua_gettable(L, -2);
        if (!lua_isnumber(L, -1)) {
            free(buf);
            return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid element index %d in bytes table.", i+1);
        }

        buf[i] = lua_tounsigned(L, -1);
        lua_pop(L, 1);
    }

    if ((ret = spi_transfer(spi, buf, buf, len)) < 0) {
        free(buf);
        return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi));
    }

    /* Convert byte buffer back to bytes table */
    for (i = 0; i < len; i++) {
        lua_pushunsigned(L, i+1);
        lua_pushunsigned(L, buf[i]);
        lua_settable(L, -3);
    }

    free(buf);

    return 1;
}
Exemplo n.º 14
0
static int lua_flash_window(lua_State *L)
{
	extern HWND ghmainframe;
	int result=0;
	int count=lua_gettop(L);
	if(count>=1){
		unsigned int i,count;
		count=lua_tounsigned(L,1);
		if(count>5)
			count=5;
		for(i=0;i<count;i++){
			FlashWindow(ghmainframe,1);
			Sleep(500);
			FlashWindow(ghmainframe,0);
			Sleep(500);
		}
		result=1;
	}
	lua_pushinteger(L,result);
	return 1;
}
Exemplo n.º 15
0
static int filter_word(lua_State *L)
{
	Table* dict = check_crab_obj(L, 1);

	luaL_checktype(L, 2, LUA_TTABLE);
	size_t len = lua_rawlen(L, 2);
	size_t i,j;
	int flag = 0;
	for(i=1;i<=len;) {
		TableNode *node = NULL;
		int step = 0;
		for(j=i;j<=len;j++) {
			lua_rawgeti(L, 2, j);
			uint32_t rune = lua_tounsigned(L, -1);
			lua_pop(L, 1);

			if(node == NULL) {
				node = table_get(dict, rune);
			} else {
				node = table_get((Table*)node->value, rune);
			}

			if(node && node->flag == 'o') step = j - i + 1;
			if(!(node && node->value)) break;
		}
		if(step > 0) {
			for(j=0;j<step;j++) {
				lua_pushinteger(L, '*');
				lua_rawseti(L, 2, i+j);
			}
			flag = 1;
			i = i + step;
		} else {
			i++;
		}
	}
	lua_pushboolean(L, flag);
	return 1;
}
Exemplo n.º 16
0
void	trap_args(lua_State *s, const char *funcname, const char *fmt, ...)
{
	int i;
	va_list v;
	const char *where;
	
	va_start(v, fmt);

	for (i = 1; *fmt; ++fmt, ++i) {
		if (lua_isnil(s, i)) break;
		switch (*fmt) {
		case 'i':
			*va_arg(v, int*) = lua_tointeger(s, i);
			break;
		case 'u':
			*va_arg(v, unsigned int*) = lua_tounsigned(s, i);
			break;
		case 'n':
			*va_arg(v, double*) = lua_tonumber(s, i);
			break;
		case 's':
			*va_arg(v, const char**) = lua_tostring(s, i);
			break;
		case 'p':
			*va_arg(v, void**) = lua_touserdata(s, i);
			break;
		case 'b':
			*va_arg(v, int*) = lua_toboolean(s, i);
			break;
		}
	}
	if (*fmt) {
		luaL_where(s, 1);
		where = lua_tostring(s, -1);
		FATAL("%s Invalid call to %s", where, funcname);
	}
	va_end(v);	
}
Exemplo n.º 17
0
/*
	table handles
	string msg
	  lightuserdata ptr
	  integer sz
 */
static int
_pack_message(lua_State *L) {
	luaL_checktype(L,1,LUA_TTABLE);
	int type = lua_type(L,2);
	void * msg = NULL;
	size_t sz = 0;
	switch(type) {
	case LUA_TSTRING: {
		const char * str = lua_tolstring(L,2,&sz);
		msg = malloc(sz);
		memcpy(msg, str, sz);
		break;
	}
	case LUA_TLIGHTUSERDATA:
		msg = lua_touserdata(L,2);
		sz = luaL_checkinteger(L,3);
		break;
	default:
		luaL_error(L, "type error : %s", lua_typename(L,type));
		break;
	}
	struct localcast *lc = malloc(sizeof(struct localcast));
	lc->n = lua_rawlen(L,1);
	uint32_t *group = malloc(lc->n * sizeof(uint32_t));
	int i;
	for (i=0;i<lc->n;i++) {
		lua_rawgeti(L,1,i+1);
		group[i] = lua_tounsigned(L,-1);
		lua_pop(L,1);
	}
	lc->msg = msg;
	lc->sz = sz;
	lc->group = group;
	lua_pushlightuserdata(L,lc);
	lua_pushinteger(L, sizeof(*lc));
	return 2;
};
Exemplo n.º 18
0
int Srt_MessageBox(lua_State* ls)
{
    const char* szMsg = NULL;
	int   nMsg        = 0;
    const char* szTitle = NULL;
    unsigned int unType = MB_OK;
    
    //信息
    if (lua_isstring(ls,1)){
        szMsg = lua_tostring(ls,1);
    }else if(lua_isnumber(ls,1)){
		nMsg=lua_tonumber(ls,1);
	}
    //标题
    if (lua_isstring(ls,2)){
        szTitle = lua_tostring(ls,2);
    }
    //对话框类型
	unsigned int unTypetmp =0;
    if (lua_isnumber(ls,3)){
        unTypetmp = lua_tounsigned(ls,3);
    }
	if (unType!=0){
		unType=unTypetmp;
	}
	int nRs;
	if (szMsg==NULL){
		char szTmp[256]={0};
		sprintf(szTmp,"%d",nMsg);
		nRs= MessageBox(NULL,szTmp,szTitle,unType);
	}else{
		nRs= MessageBox(NULL,szMsg,szTitle,unType);
	}
    lua_pushinteger(ls,nRs);
    return 1;
}
Exemplo n.º 19
0
static int lua_spi_open(lua_State *L) {
    spi_t *spi;
    const char *device;
    unsigned int mode;
    uint32_t max_speed;
    spi_bit_order_t bit_order;
    uint8_t bits_per_word;
    uint8_t extra_flags;
    int ret;

    spi = luaL_checkudata(L, 1, "periphery.SPI");

    /* Initialize file descriptor to an invalid value, in case an error occurs
     * below and gc later close()'s this object. */
    spi->fd = -1;

    /* Default settings of optional arguments */
    bit_order = MSB_FIRST;
    bits_per_word = 8;
    extra_flags = 0;

    /* Arguments passed in table form */
    if (lua_istable(L, 2)) {
        lua_getfield(L, 2, "device");
        if (!lua_isstring(L, -1))
            return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'device', should be string");
        lua_getfield(L, 2, "mode");
        if (!lua_isnumber(L, -1))
            return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'mode', should be number");
        lua_getfield(L, 2, "max_speed");
        if (!lua_isnumber(L, -1))
            return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'max_speed', should be number");

        device = lua_tostring(L, -3);
        mode = lua_tounsigned(L, -2);
        max_speed = lua_tounsigned(L, -1);

        /* Optional bit_order */
        lua_getfield(L, 2, "bit_order");
        if (lua_isstring(L, -1)) {
            const char *s;
            s = lua_tostring(L, -1);
            if (strcmp(s, "msb") == 0)
                bit_order = MSB_FIRST;
            else if (strcmp(s, "lsb") == 0)
                bit_order = LSB_FIRST;
            else
                return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid table argument 'bit_order', should be 'msb' or 'lsb'");
        } else if (!lua_isnil(L, -1)) {
            return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'bit_order', should be string");
        }

        /* Optional bits_per_word */
        lua_getfield(L, 2, "bits_per_word");
        if (lua_isnumber(L, -1))
            bits_per_word = lua_tounsigned(L, -1);
        else if (!lua_isnil(L, -1))
            return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'bits_per_word', should be number");

        /* Optional extra_flags */
        lua_getfield(L, 2, "extra_flags");
        if (lua_isnumber(L, -1))
            extra_flags = lua_tounsigned(L, -1);
        else if (!lua_isnil(L, -1))
            return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid type on table argument 'extra_flags', should be number");

    /* Arguments passed normally */
    } else {
        lua_spi_checktype(L, 2, LUA_TSTRING);
        lua_spi_checktype(L, 3, LUA_TNUMBER);
        lua_spi_checktype(L, 4, LUA_TNUMBER);

        device = lua_tostring(L, 2);
        mode = lua_tounsigned(L, 3);
        max_speed = lua_tounsigned(L, 4);
    }

    if ((ret = spi_open_advanced(spi, device, mode, max_speed, bit_order, bits_per_word, extra_flags)) < 0)
        return lua_spi_error(L, ret, spi_errno(spi), spi_errmsg(spi));

    return 0;
}
Exemplo n.º 20
0
static int AddReg(lua_State * L) {
	if(lua_gettop(L) == 3) {
        if(lua_type(L, 1) != LUA_TSTRING || lua_type(L, 2) != LUA_TSTRING || lua_type(L, 3) != LUA_TNUMBER) {
            luaL_checktype(L, 1, LUA_TSTRING);
            luaL_checktype(L, 2, LUA_TSTRING);
            luaL_checktype(L, 3, LUA_TNUMBER);
            lua_settop(L, 0);
            lua_pushnil(L);
            return 1;
        }

        size_t szNickLen, szPassLen;
        char *sNick = (char *)lua_tolstring(L, 1, &szNickLen);
        char *sPass = (char *)lua_tolstring(L, 2, &szPassLen);

#if LUA_VERSION_NUM < 503
		uint16_t i16Profile = (uint16_t)lua_tonumber(L, 3);
#else
        uint16_t i16Profile = (uint16_t)lua_tounsigned(L, 3);
#endif

        if(i16Profile > clsProfileManager::mPtr->iProfileCount-1 || szNickLen == 0 || szNickLen > 64 || szPassLen == 0 || szPassLen > 64 || strpbrk(sNick, " $|") != NULL || strchr(sPass, '|') != NULL) {
            lua_settop(L, 0);
            lua_pushnil(L);
            return 1;
        }

        bool bAdded = clsRegManager::mPtr->AddNew(sNick, sPass, i16Profile);

        lua_settop(L, 0);

        if(bAdded == false) {
            lua_pushnil(L);
            return 1;
        }

        lua_pushboolean(L, 1);
        return 1;
    } else if(lua_gettop(L) == 2) {
        if(lua_type(L, 1) != LUA_TSTRING || lua_type(L, 2) != LUA_TNUMBER) {
            luaL_checktype(L, 1, LUA_TSTRING);
            luaL_checktype(L, 2, LUA_TNUMBER);
            lua_settop(L, 0);
            lua_pushnil(L);
            return 1;
        }

        size_t szNickLen;
        char *sNick = (char *)lua_tolstring(L, 1, &szNickLen);

#if LUA_VERSION_NUM < 503
		uint16_t ui16Profile = (uint16_t)lua_tonumber(L, 2);
#else
        uint16_t ui16Profile = (uint16_t)lua_tounsigned(L, 2);
#endif

        if(ui16Profile > clsProfileManager::mPtr->iProfileCount-1 || szNickLen == 0 || szNickLen > 64 || strpbrk(sNick, " $|") != NULL) {
            lua_settop(L, 0);
            lua_pushnil(L);
            return 1;
        }

        // check if user is registered
        if(clsRegManager::mPtr->Find(sNick, szNickLen) != NULL) {
            lua_settop(L, 0);
            lua_pushnil(L);
            return 1;
        }

        User * pUser = clsHashManager::mPtr->FindUser(sNick, szNickLen);
        if(pUser == NULL) {
            lua_settop(L, 0);
            lua_pushnil(L);
            return 1;
        }

        if(pUser->uLogInOut == NULL) {
            pUser->uLogInOut = new LoginLogout();
            if(pUser->uLogInOut == NULL) {
                pUser->ui32BoolBits |= User::BIT_ERROR;
                pUser->Close();

                AppendDebugLog("%s - [MEM] Cannot allocate new pUser->uLogInOut in RegMan.AddReg\n", 0);
                lua_settop(L, 0);
                lua_pushnil(L);
                return 1;
            }
        }

        pUser->SetBuffer(clsProfileManager::mPtr->ProfilesTable[ui16Profile]->sName);
        pUser->ui32BoolBits |= User::BIT_WAITING_FOR_PASS;

        int iMsgLen = sprintf(clsServerManager::sGlobalBuffer, "<%s> %s.|$GetPass|", clsSettingManager::mPtr->sPreTexts[clsSettingManager::SETPRETXT_HUB_SEC], clsLanguageManager::mPtr->sTexts[LAN_YOU_WERE_REGISTERED_PLEASE_ENTER_YOUR_PASSWORD]);
        if(CheckSprintf(iMsgLen, clsServerManager::szGlobalBufferSize, "RegMan.AddReg1") == true) {
            pUser->SendCharDelayed(clsServerManager::sGlobalBuffer, iMsgLen);
        }

        lua_settop(L, 0);
        lua_pushboolean(L, 1);
        return 1;
    } else {
        luaL_error(L, "bad argument count to 'RegMan.AddReg' (2 or 3 expected, got %d)", lua_gettop(L));
        lua_settop(L, 0);
        lua_pushnil(L);
        return 1;
    }
}
Exemplo n.º 21
0
int _lua_rabbit_sleep(lua_State * L) {
    int argc = lua_gettop(L);
    unsigned int arg_0 = lua_tounsigned(L, 1);
    rabbit_sleep(arg_0);
    return 0;
}
Exemplo n.º 22
0
static int lua_serial_open(lua_State *L) {
    serial_t *serial;
    const char *device;
    uint32_t baudrate;
    int databits;
    serial_parity_t parity;
    int stopbits;
    bool xonxoff;
    bool rtscts;
    int ret;

    serial = luaL_checkudata(L, 1, "periphery.Serial");

    /* Initialize file descriptor to an invalid value, in case an error occurs
     * below and gc later close()'s this object. */
    serial->fd = -1;

    /* Default settings of optional arguments */
    databits = 8;
    parity = PARITY_NONE;
    stopbits = 1;
    xonxoff = false;
    rtscts = false;

    /* Arguments passed in table form */
    if (lua_istable(L, 2)) {
        lua_getfield(L, 2, "device");
        if (!lua_isstring(L, -1))
            return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: invalid type on table argument 'device', should be string");
        lua_getfield(L, 2, "baudrate");
        if (!lua_isnumber(L, -1))
            return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: invalid type on table argument 'baudrate', should be number");

        device = lua_tostring(L, -2);
        baudrate = lua_tounsigned(L, -1);

        /* Optional databits */
        lua_getfield(L, 2, "databits");
        if (lua_isnumber(L, -1))
            databits = lua_tounsigned(L, -1);
        else if (!lua_isnil(L, -1))
            return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: invalid type of table argument 'databits', should be number");

        /* Optional parity */
        lua_getfield(L, 2, "parity");
        if (lua_isstring(L, -1)) {
            const char *s = lua_tostring(L, -1);
            if (strcmp(s, "none") == 0)
                parity = PARITY_NONE;
            else if (strcmp(s, "even") == 0)
                parity = PARITY_EVEN;
            else if (strcmp(s, "odd") == 0)
                parity = PARITY_ODD;
            else
                return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: invalid parity, should be 'none', 'even', or 'odd'");
        } else if (!lua_isnil(L, -1))
            return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: invalid type of table argument 'parity', should be string");

        /* Optional stopbits */
        lua_getfield(L, 2, "stopbits");
        if (lua_isnumber(L, -1))
            stopbits = lua_tounsigned(L, -1);
        else if (!lua_isnil(L, -1))
            return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: invalid type of table argument 'stopbits', should be number");

        /* Optional xonxoff */
        lua_getfield(L, 2, "xonxoff");
        if (lua_isboolean(L, -1))
            xonxoff = lua_toboolean(L, -1);
        else if (!lua_isnil(L, -1))
            return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: invalid type of table argument 'xonxoff', should be boolean");

        /* Optional rtscts */
        lua_getfield(L, 2, "rtscts");
        if (lua_isboolean(L, -1))
            rtscts = lua_toboolean(L, -1);
        else if (!lua_isnil(L, -1))
            return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: invalid type of table argument 'rtscts', should be boolean");

    /* Arguments passed normally */
    } else {
        lua_serial_checktype(L, 2, LUA_TSTRING);
        lua_serial_checktype(L, 3, LUA_TNUMBER);

        device = lua_tostring(L, 2);
        baudrate = lua_tounsigned(L, 3);
    }

    if ((ret = serial_open_advanced(serial, device, baudrate, databits, parity, stopbits, xonxoff, rtscts)) < 0)
        return lua_serial_error(L, ret, serial_errno(serial), serial_errmsg(serial));

    return 0;
}
Exemplo n.º 23
0
static int lua_serial_newindex(lua_State *L) {
    serial_t *serial;
    const char *field;

    serial = luaL_checkudata(L, 1, "periphery.Serial");

    if (!lua_isstring(L, 2))
        return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: unknown property");

    field = lua_tostring(L, 2);

    if (strcmp(field, "fd") == 0)
        return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: immutable property");
    else if (strcmp(field, "baudrate") == 0) {
        uint32_t baudrate;
        int ret;

        lua_serial_checktype(L, 3, LUA_TNUMBER);
        baudrate = lua_tounsigned(L, 3);

        if ((ret = serial_set_baudrate(serial, baudrate)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    } else if (strcmp(field, "databits") == 0) {
        int databits;
        int ret;

        lua_serial_checktype(L, 3, LUA_TNUMBER);
        databits = lua_tounsigned(L, 3);

        if ((ret = serial_set_databits(serial, databits)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    } else if (strcmp(field, "parity") == 0) {
        const char *s;
        serial_parity_t parity;
        int ret;

        lua_serial_checktype(L, 3, LUA_TSTRING);
        s = lua_tostring(L, 3);

        if (strcmp(s, "none") == 0)
            parity = PARITY_NONE;
        else if (strcmp(s, "odd") == 0)
            parity = PARITY_ODD;
        else if (strcmp(s, "even") == 0)
            parity = PARITY_EVEN;
        else
            return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: invalid parity, should be 'none', 'even', or 'odd'");

        if ((ret = serial_set_parity(serial, parity)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    } else if (strcmp(field, "stopbits") == 0) {
        int stopbits;
        int ret;

        lua_serial_checktype(L, 3, LUA_TNUMBER);
        stopbits = lua_tounsigned(L, 3);

        if ((ret = serial_set_stopbits(serial, stopbits)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    } else if (strcmp(field, "xonxoff") == 0) {
        bool xonxoff;
        int ret;

        lua_serial_checktype(L, 3, LUA_TBOOLEAN);
        xonxoff = lua_toboolean(L, 3);

        if ((ret = serial_set_xonxoff(serial, xonxoff)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    } else if (strcmp(field, "rtscts") == 0) {
        bool rtscts;
        int ret;

        lua_serial_checktype(L, 3, LUA_TBOOLEAN);
        rtscts = lua_toboolean(L, 3);

        if ((ret = serial_set_rtscts(serial, rtscts)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    }

    return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: unknown property");
}
Exemplo n.º 24
0
static int ChangeReg(lua_State * L) {
	if(lua_gettop(L) != 3) {
        luaL_error(L, "bad argument count to 'ChangeReg' (3 expected, got %d)", lua_gettop(L));
        lua_settop(L, 0);
        lua_pushnil(L);
        return 1;
    }

    if(lua_type(L, 1) != LUA_TSTRING || lua_type(L, 3) != LUA_TNUMBER) {
        luaL_checktype(L, 1, LUA_TSTRING);
        luaL_checktype(L, 3, LUA_TNUMBER);
		lua_settop(L, 0);
		lua_pushnil(L);
        return 1;
    }

    size_t szNickLen, szPassLen = 0;
    char * sNick = (char *)lua_tolstring(L, 1, &szNickLen);
    char * sPass = NULL;

    if(lua_type(L, 2) == LUA_TSTRING) {
        char * sPass = (char *)lua_tolstring(L, 2, &szPassLen);
        if(szPassLen == 0 || szPassLen > 64 || strpbrk(sPass, "|") != NULL) {
            lua_settop(L, 0);
            lua_pushnil(L);
            return 1;
        }
    } else if(lua_type(L, 2) != LUA_TNIL) {
        luaL_checktype(L, 2, LUA_TSTRING);
		lua_settop(L, 0);
		lua_pushnil(L);
        return 1;
    }

#if LUA_VERSION_NUM < 503
	uint16_t i16Profile = (uint16_t)lua_tonumber(L, 3);
#else
	uint16_t i16Profile = (uint16_t)lua_tounsigned(L, 3);
#endif

	if(i16Profile > clsProfileManager::mPtr->iProfileCount-1 || szNickLen == 0 || szNickLen > 64 || strpbrk(sNick, " $|") != NULL) {
		lua_settop(L, 0);
		lua_pushnil(L);
        return 1;
    }

	RegUser *reg = clsRegManager::mPtr->Find(sNick, szNickLen);

    if(reg == NULL) {
		lua_settop(L, 0);
		lua_pushnil(L);
        return 1;
    }

    clsRegManager::mPtr->ChangeReg(reg, sPass, i16Profile);

    lua_settop(L, 0);

    lua_pushboolean(L, 1);
    return 1;
}
Exemplo n.º 25
0
inline T get_unsigned(std::true_type, lua_State* L, int index = -1) {
    return lua_tounsigned(L, index);
}
Exemplo n.º 26
0
int main()
{
  lua_State* state = lua_newstate(allocator, NULL);
  luaL_requiref(state, "base", luaopen_base, 1);
  char* buffer = malloc(sizeof(char) * BUFFER_SIZE);
  fprintf(stdout, "lua calculator started.\n");
  while(true)
    {
      printStackTop(state);
      fprintf(stdout, "Please select action:\n");
      fgets(buffer, BUFFER_SIZE - 1, stdin);
      int idx;
      for (idx = 0; buffer[idx] != '\n'; idx++);
      buffer[idx] = '\0';

      FILE* f = fopen(buffer, "r");
      if (f == NULL)
        {
          fprintf(stdout, "ERROR: lua script (%s) not found\n", buffer);
          continue;
        }
      load_t* loadVector = malloc(sizeof(load_t));
      loadVector->fp = f;

      int result;
      result = lua_load(state, loader, loadVector, buffer, "t");
      free(loadVector);
      fclose(f);
      if (result == LUA_ERRSYNTAX)
        {
          fprintf(stdout, "ERROR: syntax error in lua script\n");
          lua_pop(state, 1);
          continue;
        }
      
      /*
       * the script is assumed to have 2 objects:
       * 1. paramCount (int)
       * 2. a function named calc (func)
       */
      result = lua_pcall(state, 0, LUA_MULTRET, 0);
      if(result == LUA_OK)
        {
          lua_getglobal(state, "paramCount");
          if (lua_isnumber(state, -1) != 1)
            {
              fprintf(stdout, "ERROR: paramCount should be a positive integer\n");
              continue;
            }
          unsigned int paramCount = lua_tounsigned(state, -1);
          fprintf(stdout, "I need %u params.\n", paramCount);

          lua_pop(state, 1);
          lua_getglobal(state, "calc");
          if (lua_isfunction(state, -1) != 1)
            {
              fprintf(stdout, "ERROR: calc should be a function");
              continue;
            }

          int i;
          for( i = 0; i < paramCount; i++)
            {
              int param;
              fprintf(stdout, "Please give me param %d\n", i + 1);
              int scanned = fscanf(stdin, "%d", &param);
              for(;fgetc(stdin) != '\n';);
              if (scanned == 0)
                break;
              lua_pushinteger(state, param);
            }
          if (i != paramCount)
            {
              fprintf(stdout, "ERROR: parameter input not done\n");
              continue;
            }
          
          result = lua_pcall(state, paramCount, 1, 0);
          if (result != LUA_OK)
            {
              fprintf(stdout, "ERROR: when doing calculation (%d)\n", result);
              fprintf(stdout, "%s\n", lua_tostring(state, -1));
              lua_pop(state, 1);
              continue;
            }
          if (lua_isnumber(state, -1) != 1)
            {
              fprintf(stdout, "ERROR: calculation did not return a integer\n");
              continue;
            }
          fprintf(stdout, "Result: %d\n", lua_tointeger(state, -1));
          lua_pop(state, 1);
        }
      else if (result == LUA_ERRRUN)
        {
          fprintf(stdout, "ERROR: runtime error\n");
          fprintf(stdout, "%s\n", lua_tostring(state, -1));
          lua_pop(state, 1);
          continue;
        }
      else
        {
          fprintf(stdout, "ERROR: other error\n");
          fprintf(stdout, "%s\n", lua_tostring(state, -1));
          lua_pop(state, 1);
          continue;
        }
    }
  free(buffer);
  lua_close(state);
}
Exemplo n.º 27
0
static int lua_likwid_initTemp(lua_State* L)
{
    int cpuid = lua_tounsigned(L,-1);
    thermal_init(cpuid);
    return 0;
}
Exemplo n.º 28
0
inline lua_Unsigned to<lua_Unsigned>(lua_State* L, int index)
{
    LUNA_INDEX_ASSERT(L, index);
    return lua_tounsigned(L, index);
}
Exemplo n.º 29
0
/*
	unsigned address
	 string address
	integer type
	integer session
	string message
	 lightuserdata message_ptr
	 integer len
 */
static int
_send(lua_State *L) {
	struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
	int addr_type = lua_type(L,1);
	uint32_t dest = 0;
	switch(addr_type) {
	case LUA_TNUMBER:
		dest = lua_tounsigned(L,1);
		break;
	case LUA_TSTRING: {
		const char * addrname = lua_tostring(L,1);
		if (addrname[0] == '.' || addrname[0] == ':') {
			dest = skynet_queryname(context, addrname);
			if (dest == 0) {
				luaL_error(L, "Invalid name %s", addrname);
			}
		} else if ('0' <= addrname[0] && addrname[0] <= '9') {
			luaL_error(L, "Invalid name %s: must not start with a digit", addrname);
		} else {
			return _sendname(L, context, addrname);
		}
		break;
	}
	default:
		return luaL_error(L, "address must be number or string, got %s",lua_typename(L,addr_type));
	}

	int type = luaL_checkinteger(L, 2);
	int session = 0;
	if (lua_isnil(L,3)) {
		type |= PTYPE_TAG_ALLOCSESSION;
	} else {
		session = luaL_checkinteger(L,3);
	}

	int mtype = lua_type(L,4);
	switch (mtype) {
	case LUA_TSTRING: {
		size_t len = 0;
		void * msg = (void *)lua_tolstring(L,4,&len);
		if (len == 0) {
			msg = NULL;
		}
		session = skynet_send(context, 0, dest, type, session , msg, len);
		break;
	}
	case LUA_TLIGHTUSERDATA: {
		void * msg = lua_touserdata(L,4);
		int size = luaL_checkinteger(L,5);
		session = skynet_send(context, 0, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size);
		break;
	}
	default:
		luaL_error(L, "skynet.send invalid param %s", lua_typename(L, lua_type(L,4)));
	}
	if (session < 0) {
		// send to invalid address
		// todo: maybe throw error is better
		return 0;
	}
	lua_pushinteger(L,session);
	return 1;
}
Exemplo n.º 30
0
static int lua_spi_newindex(lua_State *L) {
    spi_t *spi;
    const char *field;

    spi = luaL_checkudata(L, 1, "periphery.SPI");

    if (!lua_isstring(L, 2))
        return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: unknown property");

    field = lua_tostring(L, 2);

    if (strcmp(field, "fd") == 0)
        return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: immutable property");
    else if (strcmp(field, "mode") == 0) {
        unsigned int mode;
        int ret;

        lua_spi_checktype(L, 3, LUA_TNUMBER);
        mode = lua_tounsigned(L, 3);

        if ((ret = spi_set_mode(spi, mode)) < 0)
            return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi));

        return 0;
    } else if (strcmp(field, "max_speed") == 0) {
        uint32_t max_speed;
        int ret;

        lua_spi_checktype(L, 3, LUA_TNUMBER);
        max_speed = lua_tounsigned(L, 3);

        if ((ret = spi_set_max_speed(spi, max_speed)) < 0)
            return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi));

        return 0;
    } else if (strcmp(field, "bit_order") == 0) {
        const char *s;
        spi_bit_order_t bit_order;
        int ret;

        lua_spi_checktype(L, 3, LUA_TSTRING);

        s = lua_tostring(L, 3);
        if (strcmp(s, "msb") == 0)
            bit_order = MSB_FIRST;
        else if (strcmp(s, "lsb") == 0)
            bit_order = LSB_FIRST;
        else
            return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: invalid bit_order, should be 'msb' or 'lsb'");

        if ((ret = spi_set_bit_order(spi, bit_order)) < 0)
            return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi));

        return 0;
    } else if (strcmp(field, "bits_per_word") == 0) {
        uint8_t bits_per_word;
        int ret;

        lua_spi_checktype(L, 3, LUA_TNUMBER);
        bits_per_word = lua_tounsigned(L, 3);

        if ((ret = spi_set_bits_per_word(spi, bits_per_word)) < 0)
            return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi));

        return 0;
    } else if (strcmp(field, "extra_flags") == 0) {
        uint8_t extra_flags;
        int ret;

        lua_spi_checktype(L, 3, LUA_TNUMBER);
        extra_flags = lua_tounsigned(L, 3);

        if ((ret = spi_set_extra_flags(spi, extra_flags)) < 0)
            return lua_spi_error(L, ret, spi_errno(spi), "Error: %s", spi_errmsg(spi));

        return 0;
    }

    return lua_spi_error(L, SPI_ERROR_ARG, 0, "Error: unknown property");
}