Esempio n. 1
0
static char *
_get_value(lua_State *L, int index, char * ptr, char type) {
	switch(type) {
		case 'i': {
			int32_t v = luaL_checkinteger(L, index);
			memcpy(ptr, &v, 4);
			return ptr + 4;
		}
		case 'u': {
			uint64_t v = (uint64_t)luaL_checknumber(L, index);
			memcpy(ptr, &v, 8);
			return ptr + 8;
		}
		case 'd': {
			int64_t v = (int64_t)luaL_checknumber(L, index);
			memcpy(ptr, &v, 8);
			return ptr + 8;
		}
		case 'b': {
			int32_t v = lua_toboolean(L, index);
			memcpy(ptr, &v, 4);
			return ptr + 4;
		}
		case 'p': {
			void *p = lua_touserdata(L, index);
			uint32_t v = (uint32_t)(intptr_t)p;
			memcpy(ptr, &v , 4);
			return ptr + 4;
		}
		case 'x': {
			const char * i64 = luaL_checkstring(L, index);
			memcpy(ptr, i64, 8);
			return ptr + 8;
		}
		case 'r': {
			double v = luaL_checknumber(L, index);
			memcpy(ptr, &v, 8);
			return ptr + 8;
		}
		case 's': {
			size_t sz = 0;
			const char * str = luaL_checklstring(L, index, &sz);
			struct pbc_slice * slice = (struct pbc_slice *)ptr;
			slice->buffer = (void*)str;
			slice->len = sz;
			return ptr + sizeof(struct pbc_slice);
		}
		case 'm': {
			struct pbc_slice * slice = (struct pbc_slice *)ptr;
			if (lua_istable(L,index)) {
				lua_rawgeti(L,index,1);
				slice->buffer = lua_touserdata(L,-1);
				lua_rawgeti(L,index,2);
				slice->len = lua_tointeger(L,-1);
				lua_pop(L,2);
			} else {
				size_t sz = 0;
				const char * buffer = luaL_checklstring(L, index, &sz);
				slice->buffer = (void *)buffer;
				slice->len = sz;
			}
			return ptr + sizeof(struct pbc_slice);
		}
		default:
			luaL_error(L,"unknown format %c", type);
			return ptr;
	}
}
Esempio n. 2
0
static int color_setBackground(lua_State *L) {
	color_background = luaL_checkinteger(L, 1);
	sf2d_set_clear_color(color_background);

	return 0;
}
Esempio n. 3
0
File: fs.c Progetto: ifzz/LuaIO
/*local err = fs.close(fd)*/
static int LuaIO_fs_close(lua_State* L) {
  int fd = luaL_checkinteger(L, 1);

  CREATE_REQ1();
  FS_CALL1(close, req, fd);
}
Esempio n. 4
0
static int make_lua_call(lua_State *L, mrp_funcbridge_t *fb, int f)
{
#define ARG_MAX 256

    int ret;
    int i, n, m, b, e;
    const char *s;
    char t;
    mrp_funcbridge_value_t args[ARG_MAX];
    mrp_funcbridge_value_t *a, r;

    e = lua_gettop(L);
    f = (f < 0) ? e + f + 1 : f;
    b = f + 1;
    n = e - b + 1;

    switch (fb->type) {

    case MRP_C_FUNCTION:
        m = strlen(fb->c.signature);

        if (n >= ARG_MAX - 1 || n > m)
            return luaL_error(L, "too many arguments");
        if (n < m)
            return luaL_error(L, "too few arguments");

        for (i = b, s = fb->c.signature, a= args;    i <= e;    i++, s++, a++){
            switch (*s) {
            case MRP_FUNCBRIDGE_STRING:
                a->string = luaL_checklstring(L, i, NULL);
                break;
            case MRP_FUNCBRIDGE_INTEGER:
                a->integer = luaL_checkinteger(L, i);
                break;
            case MRP_FUNCBRIDGE_FLOATING:
                a->floating = luaL_checknumber(L, i);
                break;
            case MRP_FUNCBRIDGE_OBJECT:
                a->pointer = mrp_lua_check_object(L, NULL, i);
                break;
            default:
                return luaL_error(L, "argument %d has unsupported type '%c'",
                                  (i - b) + 1, i);
            }
        }
        memset(a, 0, sizeof(*a));

        if (!fb->c.func(L, fb->c.data, fb->c.signature, args, &t, &r))
            return luaL_error(L, "c function invocation failed");

        switch (t) {
        case MRP_FUNCBRIDGE_NO_DATA:
            ret = 0;
            break;
        case MRP_FUNCBRIDGE_STRING:
            ret = 1;
            lua_pushstring(L, r.string);
            break;
        case MRP_FUNCBRIDGE_INTEGER:
            ret = 1;
            lua_pushinteger(L, r.integer);
            break;
        case MRP_FUNCBRIDGE_FLOATING:
            ret = 1;
            lua_pushnumber(L, r.floating);
            break;
        default:
            ret = 0;
            lua_pushnil(L);
        }
        break;

    case MRP_LUA_FUNCTION:
        lua_rawgeti(L, f, 1);
        luaL_checktype(L, -1, LUA_TFUNCTION);
        lua_replace(L, f);
        lua_pcall(L, n, 1, 0);
        ret = 1;
        break;

    default:
        return luaL_error(L, "internal error");
    }

    return ret;

#undef ARG_MAX
}
Esempio n. 5
0
static int gprs_start( lua_State* L )
{
uint32_t len;
int timeout = luaL_checkinteger(L, 1);
const char *APN[10];
char *APN_USED;
char Param[BUFFER_SZ];
uint8_t i = 0;
uint8_t j = 5;
uint8_t result = 1;
unsigned char cont = lua_gettop(L) - 1;

	APN[0] = lua_tolstring( L, 2, &len );
	APN[1] = lua_tolstring( L, 3, &len );
	APN[2] = lua_tolstring( L, 4, &len );
	APN[3] = lua_tolstring( L, 5, &len );
	APN[4] = lua_tolstring( L, 6, &len );
	APN[5] = lua_tolstring( L, 7, &len );
	APN[6] = lua_tolstring( L, 8, &len );
	APN[7] = lua_tolstring( L, 9, &len );
	APN[8] = lua_tolstring( L, 10, &len );
	APN[9] = lua_tolstring( L, 11, &len );
	c_memset(Param, 0 , BUFFER_SZ);
	if( APN[0] != NULL && APN[1] != NULL && APN[2] != NULL && APN[3] != NULL )
	{
		result = 1;
		gprs_clear(L);
		milisec(1500);
		tcpip_getIpStatus(L);
		len = gprs_Attach(L);
		if( len == 1 )
		{
			result = 2;
			for(i = 0; i < 5; i++)
			{
				gprs_clear(L);
				//tcpip_getIpStatus(L);
				if( APN[9] != NULL )
				{
					APN_USED = (char*)APN[9];
					c_sprintf(Param, CGDCONT, APN[9]);
					for(j = 0; j < 5; j++)
					{
						len = gprs_Activate(L,Param);
						if( len == 1 )
							goto end_act;

						milisec(1000);
					}
				}
				if( j>= 5 )
				{
					for(j = 0; j < cont; j++)
					{
						if( APN[j] != NULL )
						{
							APN_USED = (char*)APN[j];
							c_sprintf(Param, CGDCONT, APN[j]);
							len = gprs_Activate(L,Param);
							if( len == 1 )
								goto end_act;

							milisec(1000);
						}
					}
				}
			}
end_act:
			if( tcpip_getIpStatus(L) == 1 )
			{
				tcpip_getIp(L);
				tcpip_getIpStatus(L);
				result = 0;
				lua_pushinteger(L, result);
				lua_pushstring(L,APN_USED);
				return 2;
			}
		}
	}

	lua_pushinteger(L, result);
	return 1;
}
Esempio n. 6
0
static int image_newindex(lua_State *L)
{
  const char* membername = lua_tostring(L, -2);
  dt_image_t * my_image=checkwriteimage(L,-3);
  if(luaA_struct_has_member_name(L,dt_image_t,membername))
  {
    if(luaA_type_has_to_func(luaA_struct_typeof_member_name(L,dt_image_t,membername)))
    {
      luaA_struct_to_member_name(L, dt_image_t, my_image, membername,-1);
    }
    else
    {
      releasewriteimage(L,my_image);
      luaL_error(L,"%s is read only",membername);
    }
    releasewriteimage(L,my_image);
    return 0;
  }
  switch(luaL_checkoption(L,-2,NULL,image_fields_name))
  {
    case RATING:
      {
        int my_score = luaL_checkinteger(L,-1);
        if(my_score > 5)
        {
          releasewriteimage(L,my_image);
          return luaL_error(L,"rating too high : %d",my_score);
        }
        if(my_score == -1) my_score = 6;
        if(my_score < -1)
        {
          releasewriteimage(L,my_image);
          return luaL_error(L,"rating too low : %d",my_score);
        }
        my_image->flags &= ~0x7;
        my_image->flags |= my_score;
        break;
      }

    case CREATOR:
      dt_metadata_set(my_image->id,"Xmp.dc.creator",luaL_checkstring(L,-1));
      dt_image_synch_xmp(my_image->id);
      break;
    case PUBLISHER:
      dt_metadata_set(my_image->id,"Xmp.dc.publisher",luaL_checkstring(L,-1));
      dt_image_synch_xmp(my_image->id);
      break;
    case TITLE:
      dt_metadata_set(my_image->id,"Xmp.dc.title",luaL_checkstring(L,-1));
      dt_image_synch_xmp(my_image->id);
      break;
    case DESCRIPTION:
      dt_metadata_set(my_image->id,"Xmp.dc.description",luaL_checkstring(L,-1));
      dt_image_synch_xmp(my_image->id);
      break;
    case RIGHTS:
      dt_metadata_set(my_image->id,"Xmp.dc.title",luaL_checkstring(L,-1));
      dt_image_synch_xmp(my_image->id);
      break;
    default:
      releasewriteimage(L,my_image);
      return luaL_error(L,"unknown index for image : ",lua_tostring(L,-2));

  }
  releasewriteimage(L,my_image);
  return 0;
}
Esempio n. 7
0
/*
** Traversal function for 'ipairs' for raw tables
*/
static int ipairsaux_raw (lua_State *L) {
  lua_Integer i = luaL_checkinteger(L, 2) + 1;
  luaL_checktype(L, 1, LUA_TTABLE);
  lua_pushinteger(L, i);
  return (lua_rawgeti(L, 1, i) == LUA_TNIL) ? 1 : 2;
}
Esempio n. 8
0
	static int Menu_DescAlignment( lua_State *L ) {
		menuDef_t *menu = CheckMenu( L, 1 );
		int value = luaL_checkinteger( L, 2 );
		menu->descAlignment = value;
		return 0;
	}
Esempio n. 9
0
	static int Menu_SetDescScale( lua_State *L ) {
		menuDef_t *menu = CheckMenu( L, 1 );
		int value = luaL_checkinteger( L, 2 );
		menu->descScale = value;
		return 0;
	}
Esempio n. 10
0
File: ftp.c Progetto: maplefish/MICO
//ftp.list(ltype, otype [,dir])
//===================================
static int lftp_list( lua_State* L )
{
  int err = 0;
  uint16_t dptr = 0;
  uint8_t n = 0;
  uint8_t i = 0;
  int nlin = 0;
  char buf[255] = {0};
  uint32_t tmo;

  uint8_t ltype = luaL_checkinteger(L, 1);
  uint8_t otype = luaL_checkinteger(L, 2);
  if (otype == 1) ltype = 1;
  err = _getFName(L, 3);
  err = 0;
  
  if ((gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED))) {
    ftp_log("[FTP usr] Login first\r\n" );
    err = -1;
    goto exit;
  }

  list_type = ltype;
  data_done = 0;
  ftpCmdSocket->clientFlag = REQ_ACTION_LIST;
  
  if (ftpCmdSocket->list_cb != LUA_NOREF) {
    goto exit;
  }
  
  // no cb function, wait until List received (max 10 sec)
  tmo = mico_get_time();
  while (data_done == 0) {
    if ((mico_get_time() - tmo) > 10000) break;
    mico_thread_msleep(60);
    luaWdgReload();
  }
  if (data_done == 0) {
    ftp_log("[FTP usr] Timeout: list not received\r\n" );
    err = -3;
    goto exit;
  }

  if ((recvDataBuf == NULL) || (recvDataLen == 0)) {
    ftp_log("[FTP usr] List not received\r\n" );
    err = -4;
    goto exit;
  }
  
  if (otype != 1) {
    printf("===================\r\n");
    printf("FTP directory list:");
    if (recvDataLen == (max_recv_datalen-16)) {
      printf(" (buffer full)");
    }
    printf("\r\n");
  }
  else {
    lua_newtable( L );
  }

  while (dptr < recvDataLen) {
    if (*(recvDataBuf+dptr) == '\0') break;
    if ((*(recvDataBuf+dptr) == '\n') || (*(recvDataBuf+dptr) == '\r') || (n >= 254)) {
      // EOL, print line
      if (n > 0) {
        nlin++;
        if (otype != 1) printf("%s\r\n", &buf[0]);
        else {
          lua_pushstring( L, &buf[0] );
          lua_rawseti(L,-2,i++);
        }
        n = 0;
      }
    }
    if (*(recvDataBuf+dptr) >= ' ') buf[n++] = *(recvDataBuf+dptr);
    buf[n] = '\0';
    dptr++;
  }
  if (n > 0) { // last line
    nlin++;
    if (otype != 1) printf("%s\r\n", &buf[0]);
    else {
      lua_pushstring( L, &buf[0] );
      lua_rawseti(L,-2,i++);
    }
  }
  if (otype != 1) {
    printf("===================\r\n");
    lua_pushinteger(L, nlin);
    return 1;
  }
  else {
    ftp_log("[FTP usr] List received to table\r\n" );
    lua_pushinteger(L, nlin);
    return 2;
  }
  
exit:
  if (otype == 1) {
    lua_newtable( L );
    lua_pushinteger(L, err);
    return 2;
  }
  lua_pushinteger(L, err);
  return 1;
}
Esempio n. 11
0
	static int Menu_Border( lua_State *L ) {
		menuDef_t *menu = CheckMenu( L, 1 );
		int value = luaL_checkinteger( L, 2 );
		menu->window.border = value;
		return 0;
	}
Esempio n. 12
0
File: ftp.c Progetto: maplefish/MICO
//stat = ftp.new(serv,port,user,pass [,buflen])
//=================================
static int lftp_new( lua_State* L )
{

  size_t dlen=0, ulen=0, plen=0;
  _ftp_deinit(1);
  gL = NULL;
  
  const char *domain = luaL_checklstring( L, 1, &dlen );
  if (dlen>128 || domain == NULL) {
    ftp_log("[FTP usr] Domain needed\r\n" );
    lua_pushinteger(L, -1);
    return 1;
  }
  int port = luaL_checkinteger( L, 2 );
  const char *user = luaL_checklstring( L, 3, &ulen );
  if (ulen>128 || user == NULL) {
    ftp_log("[FTP usr] Eser needed\r\n" );
    lua_pushinteger(L, -2);
    return 1;
  }
  const char *pass = luaL_checklstring( L, 4, &plen );
  if (plen>128 || pass == NULL)  {
    ftp_log("[FTP usr] Pass needed\r\n" );
    lua_pushinteger(L, -3);
    return 1;
  }
  max_recv_datalen = 1024;
  if (lua_gettop(L) >= 5) {
    int maxdl = luaL_checkinteger(L, 5);
    if ((maxdl >= 512) && (maxdl <= 4096)) max_recv_datalen = (uint16_t)maxdl;
  }

  // allocate buffers
  pDomain4Dns=(char*)malloc(dlen+1);
  ftpuser=(char*)malloc(ulen+1);
  ftppass=(char*)malloc(plen+1);
  if ((pDomain4Dns==NULL) || (ftpuser==NULL) || (ftppass==NULL)) {
    _ftp_deinit(0);    
    ftp_log("[FTP usr] Memory allocation failed\r\n" );
    lua_pushinteger(L, -4);
    return 1;
  }
  strcpy(pDomain4Dns,domain);
  strcpy(ftpuser,user);
  strcpy(ftppass,pass);

  int socketHandle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (socketHandle < 0) {
    _ftp_deinit(0);    
    ftp_log("[FTP usr] Open CMD socket failed\r\n" );
    lua_pushinteger(L, -5);
    return 1;
  }
  
  ftpCmdSocket = NULL;
  ftpDataSocket = NULL;
  
  ftpCmdSocket = (ftpCmdSocket_t*)malloc(sizeof(ftpCmdSocket_t));
  if (ftpCmdSocket == NULL) {
    _ftp_deinit(0);    
    ftp_log("[FTP usr] Memory allocation failed\r\n" );
    lua_pushinteger(L, -6);
    return 1;
  }

  ftpCmdSocket->socket = socketHandle;
  ftpCmdSocket->disconnect_cb = LUA_NOREF;
  ftpCmdSocket->logon_cb = LUA_NOREF;
  ftpCmdSocket->list_cb = LUA_NOREF;
  ftpCmdSocket->received_cb = LUA_NOREF;
  ftpCmdSocket->sent_cb = LUA_NOREF;
  ftpCmdSocket->clientFlag = NO_ACTION;
  ftpCmdSocket->clientLastFlag = NO_ACTION;
  ftpCmdSocket->addr.s_port = port;
  status = FTP_NOT_CONNECTED;
  uint32_t opt=0;
  int err = setsockopt(socketHandle,IPPROTO_IP,SO_BLOCKMODE,&opt,4); // non block mode
  if (err < 0) {
    _ftp_deinit(0);    
    ftp_log("[FTP usr] Set socket options failed\r\n" );
    lua_pushinteger(L, -7);
    return 1;
  }

  gL = L;
  ftp_log("[FTP usr] FTP client configured.\r\n" );
  lua_pushinteger(L, 0);
  return 1;
}
static int
ngx_http_lua_shdict_set_helper(lua_State *L, int flags)
{
    int                          i, n;
    ngx_str_t                    key;
    uint32_t                     hash;
    ngx_int_t                    rc;
    ngx_http_lua_shdict_ctx_t   *ctx;
    ngx_http_lua_shdict_node_t  *sd;
    ngx_str_t                    value;
    int                          value_type;
    double                       num;
    u_char                       c;
    lua_Number                   exptime = 0;
    u_char                      *p;
    ngx_rbtree_node_t           *node;
    ngx_time_t                  *tp;
    ngx_shm_zone_t              *zone;
    int                          forcible = 0;
                         /* indicates whether to foricibly override other
                          * valid entries */
    int32_t                      user_flags = 0;

    n = lua_gettop(L);

    if (n != 3 && n != 4 && n != 5) {
        return luaL_error(L, "expecting 3, 4 or 5 arguments, "
                          "but only seen %d", n);
    }

    zone = lua_touserdata(L, 1);
    if (zone == NULL) {
        return luaL_error(L, "bad \"zone\" argument");
    }

    ctx = zone->data;

    if (lua_isnil(L, 2)) {
        lua_pushnil(L);
        lua_pushliteral(L, "nil key");
        return 2;
    }

    key.data = (u_char *) luaL_checklstring(L, 2, &key.len);

    if (key.len == 0) {
        lua_pushnil(L);
        lua_pushliteral(L, "empty key");
        return 2;
    }

    if (key.len > 65535) {
        lua_pushnil(L);
        lua_pushliteral(L, "key too long");
        return 2;
    }

    hash = ngx_crc32_short(key.data, key.len);

    value_type = lua_type(L, 3);

    switch (value_type) {
    case LUA_TSTRING:
        value.data = (u_char *) lua_tolstring(L, 3, &value.len);
        break;

    case LUA_TNUMBER:
        value.len = sizeof(double);
        num = lua_tonumber(L, 3);
        value.data = (u_char *) &num;
        break;

    case LUA_TBOOLEAN:
        value.len = sizeof(u_char);
        c = lua_toboolean(L, 3) ? 1 : 0;
        value.data = &c;
        break;

    case LUA_TNIL:
        if (flags & (NGX_HTTP_LUA_SHDICT_ADD|NGX_HTTP_LUA_SHDICT_REPLACE)) {
            lua_pushnil(L);
            lua_pushliteral(L, "attempt to add or replace nil values");
            return 2;
        }

        ngx_str_null(&value);
        break;

    default:
        lua_pushnil(L);
        lua_pushliteral(L, "bad value type");
        return 2;
    }

    if (n >= 4) {
        exptime = luaL_checknumber(L, 4);
        if (exptime < 0) {
            exptime = 0;
        }
    }

    if (n == 5) {
        user_flags = (uint32_t) luaL_checkinteger(L, 5);
    }

    ngx_shmtx_lock(&ctx->shpool->mutex);

#if 1
    ngx_http_lua_shdict_expire(ctx, 1);
#endif

    rc = ngx_http_lua_shdict_lookup(zone, hash, key.data, key.len, &sd);

    dd("shdict lookup returned %d", (int) rc);

    if (flags & NGX_HTTP_LUA_SHDICT_REPLACE) {

        if (rc == NGX_DECLINED || rc == NGX_DONE) {
            ngx_shmtx_unlock(&ctx->shpool->mutex);

            lua_pushboolean(L, 0);
            lua_pushliteral(L, "not found");
            lua_pushboolean(L, forcible);
            return 3;
        }

        /* rc == NGX_OK */

        goto replace;
    }

    if (flags & NGX_HTTP_LUA_SHDICT_ADD) {

        if (rc == NGX_OK) {
            ngx_shmtx_unlock(&ctx->shpool->mutex);

            lua_pushboolean(L, 0);
            lua_pushliteral(L, "exists");
            lua_pushboolean(L, forcible);
            return 3;
        }

        if (rc == NGX_DONE) {
            /* exists but expired */

            dd("go to replace");
            goto replace;
        }

        /* rc == NGX_DECLINED */

        dd("go to insert");
        goto insert;
    }

    if (rc == NGX_OK || rc == NGX_DONE) {

        if (value_type == LUA_TNIL) {
            goto remove;
        }

replace:
        if (value.data && value.len == (size_t) sd->value_len) {

            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
                           "lua shared dict set: found old entry and value "
                           "size matched, reusing it");

            ngx_queue_remove(&sd->queue);
            ngx_queue_insert_head(&ctx->sh->queue, &sd->queue);

            sd->key_len = (u_short) key.len;

            if (exptime > 0) {
                tp = ngx_timeofday();
                sd->expires = (uint64_t) tp->sec * 1000 + tp->msec
                              + (uint64_t) (exptime * 1000);

            } else {
                sd->expires = 0;
            }

            sd->user_flags = user_flags;

            sd->value_len = (uint32_t) value.len;

            dd("setting value type to %d", value_type);

            sd->value_type = (uint8_t) value_type;

            p = ngx_copy(sd->data, key.data, key.len);
            ngx_memcpy(p, value.data, value.len);

            ngx_shmtx_unlock(&ctx->shpool->mutex);

            lua_pushboolean(L, 1);
            lua_pushnil(L);
            lua_pushboolean(L, forcible);
            return 3;
        }

        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
                       "lua shared dict set: found old entry bug value size "
                       "NOT matched, removing it first");

remove:
        ngx_queue_remove(&sd->queue);

        node = (ngx_rbtree_node_t *)
                   ((u_char *) sd - offsetof(ngx_rbtree_node_t, color));

        ngx_rbtree_delete(&ctx->sh->rbtree, node);

        ngx_slab_free_locked(ctx->shpool, node);

    }

insert:
    /* rc == NGX_DECLINED or value size unmatch */

    if (value.data == NULL) {
        ngx_shmtx_unlock(&ctx->shpool->mutex);

        lua_pushboolean(L, 1);
        lua_pushnil(L);
        lua_pushboolean(L, 0);
        return 3;
    }

    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
                   "lua shared dict set: creating a new entry");

    n = offsetof(ngx_rbtree_node_t, color)
        + offsetof(ngx_http_lua_shdict_node_t, data)
        + key.len
        + value.len;

    node = ngx_slab_alloc_locked(ctx->shpool, n);

    if (node == NULL) {

        if (flags & NGX_HTTP_LUA_SHDICT_SAFE_STORE) {
            ngx_shmtx_unlock(&ctx->shpool->mutex);

            lua_pushboolean(L, 0);
            lua_pushliteral(L, "no memory");
            return 2;
        }

        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
                       "lua shared dict set: overriding non-expired items "
                       "due to memory shortage for entry \"%V\"", &key);

        for (i = 0; i < 30; i++) {
            if (ngx_http_lua_shdict_expire(ctx, 0) == 0) {
                break;
            }

            forcible = 1;

            node = ngx_slab_alloc_locked(ctx->shpool, n);
            if (node != NULL) {
                goto allocated;
            }
        }

        ngx_shmtx_unlock(&ctx->shpool->mutex);

        lua_pushboolean(L, 0);
        lua_pushliteral(L, "no memory");
        lua_pushboolean(L, forcible);
        return 3;
    }

allocated:
    sd = (ngx_http_lua_shdict_node_t *) &node->color;

    node->key = hash;
    sd->key_len = (u_short) key.len;

    if (exptime > 0) {
        tp = ngx_timeofday();
        sd->expires = (uint64_t) tp->sec * 1000 + tp->msec
                      + (uint64_t) (exptime * 1000);

    } else {
        sd->expires = 0;
    }

    sd->user_flags = user_flags;

    sd->value_len = (uint32_t) value.len;

    dd("setting value type to %d", value_type);

    sd->value_type = (uint8_t) value_type;

    p = ngx_copy(sd->data, key.data, key.len);
    ngx_memcpy(p, value.data, value.len);

    ngx_rbtree_insert(&ctx->sh->rbtree, node);

    ngx_queue_insert_head(&ctx->sh->queue, &sd->queue);

    ngx_shmtx_unlock(&ctx->shpool->mutex);

    lua_pushboolean(L, 1);
    lua_pushnil(L);
    lua_pushboolean(L, forcible);
    return 3;
}
Esempio n. 14
0
static void
_get_array_value(lua_State *L, pbc_array array, char type) {
	switch(type) {
		case 'I': {
			int32_t v = luaL_checkinteger(L, -1);
			uint32_t hi = 0;
			if (v<0) {
				hi = ~0;
			}
			pbc_array_push_integer(array, v, hi);
			break;
		}
		case 'U' : {
			uint64_t v = (uint64_t)luaL_checknumber(L, -1);
			pbc_array_push_integer(array, (uint32_t)v, (uint32_t)(v >> 32));
			break;
		}
		case 'D' : {
			int64_t v = (int64_t)luaL_checknumber(L, -1);
			pbc_array_push_integer(array, (uint32_t)v, (uint32_t)(v >> 32));
			break;
		}
		case 'B': {
			int32_t v = lua_toboolean(L, -1);
			pbc_array_push_integer(array, v ? 1: 0, 0);
			break;
		}
		case 'P': {
			void *p = lua_touserdata(L, -1);
			uint32_t v = (uint32_t)(intptr_t)p;
			pbc_array_push_integer(array, v, 0);
			break;
		}
		case 'X': {
			const char * i64 = luaL_checkstring(L, -1);
			uint64_t v = *(uint64_t *)i64;
			pbc_array_push_integer(array, (uint32_t)v, (uint32_t)(v >> 32));
			break;
		}
		case 'R': {
			double v = luaL_checknumber(L, -1);
			pbc_array_push_real(array, v);
			break;
		}
		case 'S': {
			size_t sz = 0;
			const char * str = luaL_checklstring(L, -1, &sz);
			struct pbc_slice slice;
			slice.buffer = (void*)str;
			slice.len = sz;
			pbc_array_push_slice(array, &slice);
			break;
		}
		case 'M': {
			struct pbc_slice slice;
			if (lua_istable(L,-1)) {
				lua_rawgeti(L,-1,1);
				slice.buffer = lua_touserdata(L,-1);
				lua_rawgeti(L,-2,2);
				slice.len = lua_tointeger(L,-1);
				lua_pop(L,2);
			} else {
				size_t sz = 0;
				const char * buffer = luaL_checklstring(L, -1, &sz);
				slice.buffer = (void *)buffer;
				slice.len = sz;
			}
			pbc_array_push_slice(array, &slice);
			break;
		}
	}
}
Esempio n. 15
0
static int math_ldexp (lua_State *L) {
  lua_Number x = luaL_checknumber(L, 1);
  int ep = (int)luaL_checkinteger(L, 2);
  lua_pushnumber(L, l_mathop(ldexp)(x, ep));
  return 1;
}
Esempio n. 16
0
	static int Menu_FadeCycle( lua_State *L ) {
		menuDef_t *menu = CheckMenu( L, 1 );
		int value = luaL_checkinteger( L, 2 );
		menu->fadeCycle = value;
		return 0;
	}
Esempio n. 17
0
static int mpz_power(lua_State * L) {
    int k = luaL_checkinteger(L, 2);
    if (k < 0) throw exception("argument #2 must be positive");
    return push_mpz(L, pow(to_mpz_ext(L, 1), k));
}
Esempio n. 18
0
	static int Menu_OwnerDraw( lua_State *L ) {
		menuDef_t *menu = CheckMenu( L, 1 );
		int value = luaL_checkinteger( L, 2 );
		menu->window.ownerDraw = value;
		return 0;
	}
static int
ngx_http_lua_socket_udp_setpeername(lua_State *L)
{
    ngx_http_request_t          *r;
    ngx_http_lua_ctx_t          *ctx;
    ngx_str_t                    host;
    int                          port;
    ngx_resolver_ctx_t          *rctx, temp;
    ngx_http_core_loc_conf_t    *clcf;
    int                          saved_top;
    int                          n;
    u_char                      *p;
    size_t                       len;
    ngx_url_t                    url;
    ngx_int_t                    rc;
    ngx_http_lua_loc_conf_t     *llcf;
    ngx_udp_connection_t        *uc;
    int                          timeout;

    ngx_http_lua_socket_udp_upstream_t      *u;

    n = lua_gettop(L);
    if (n != 2 && n != 3) {
        return luaL_error(L, "ngx.socket.udp setpeername: expecting 2 or 3 "
                          "arguments (including the object), but seen %d", n);
    }

    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
    lua_rawget(L, LUA_GLOBALSINDEX);
    r = lua_touserdata(L, -1);
    lua_pop(L, 1);

    if (r == NULL) {
        return luaL_error(L, "no request found");
    }

    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
    if (ctx == NULL) {
        return luaL_error(L, "no ctx found");
    }

    ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
                               | NGX_HTTP_LUA_CONTEXT_ACCESS
                               | NGX_HTTP_LUA_CONTEXT_CONTENT);

    luaL_checktype(L, 1, LUA_TTABLE);

    p = (u_char *) luaL_checklstring(L, 2, &len);

    host.data = ngx_palloc(r->pool, len + 1);
    if (host.data == NULL) {
        return luaL_error(L, "out of memory");
    }

    host.len = len;

    ngx_memcpy(host.data, p, len);
    host.data[len] = '\0';

    if (n == 3) {
        port = luaL_checkinteger(L, 3);

        if (port < 0 || port > 65536) {
            lua_pushnil(L);
            lua_pushfstring(L, "bad port number: %d", port);
            return 2;
        }

    } else { /* n == 2 */
        port = 0;
    }

    lua_rawgeti(L, 1, SOCKET_CTX_INDEX);
    u = lua_touserdata(L, -1);
    lua_pop(L, 1);

    if (u) {
        if (u->waiting) {
            lua_pushnil(L);
            lua_pushliteral(L, "socket busy");
            return 2;
        }

        if (u->udp_connection.connection) {
            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                           "lua udp socket reconnect without shutting down");

            ngx_http_lua_socket_udp_finalize(r, u);
        }

        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "lua reuse socket upstream ctx");

    } else {
        u = lua_newuserdata(L, sizeof(ngx_http_lua_socket_udp_upstream_t));
        if (u == NULL) {
            return luaL_error(L, "out of memory");
        }

#if 1
        lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
        lua_pushcfunction(L, ngx_http_lua_socket_udp_upstream_destroy);
        lua_setfield(L, -2, "__gc");
        lua_setmetatable(L, -2);
#endif

        lua_rawseti(L, 1, SOCKET_CTX_INDEX);
    }

    ngx_memzero(u, sizeof(ngx_http_lua_socket_udp_upstream_t));

    u->request = r; /* set the controlling request */
    llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);

    u->conf = llcf;

    uc = &u->udp_connection;

    uc->log = *r->connection->log;

    dd("lua peer connection log: %p", &uc->log);

    lua_rawgeti(L, 1, SOCKET_TIMEOUT_INDEX);
    timeout = (ngx_int_t) lua_tointeger(L, -1);
    lua_pop(L, 1);

    if (timeout > 0) {
        u->read_timeout = (ngx_msec_t) timeout;

    } else {
        u->read_timeout = u->conf->read_timeout;
    }

    ngx_memzero(&url, sizeof(ngx_url_t));

    url.url.len = host.len;
    url.url.data = host.data;
    url.default_port = port;
    url.no_resolve = 1;

    if (ngx_parse_url(r->pool, &url) != NGX_OK) {
        lua_pushnil(L);

        if (url.err) {
            lua_pushfstring(L, "failed to parse host name \"%s\": %s",
                            host.data, url.err);

        } else {
            lua_pushfstring(L, "failed to parse host name \"%s\"", host.data);
        }

        return 2;
    }

    u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
    if (u->resolved == NULL) {
        return luaL_error(L, "out of memory");
    }

    if (url.addrs && url.addrs[0].sockaddr) {
        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "lua udp socket network address given directly");

        u->resolved->sockaddr = url.addrs[0].sockaddr;
        u->resolved->socklen = url.addrs[0].socklen;
        u->resolved->naddrs = 1;
        u->resolved->host = url.addrs[0].name;

    } else {
        u->resolved->host = host;
        u->resolved->port = (in_port_t) port;
    }

    if (u->resolved->sockaddr) {
        rc = ngx_http_lua_socket_resolve_retval_handler(r, u, L);
        if (rc == NGX_AGAIN) {
            return lua_yield(L, 0);
        }

        return rc;
    }

    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

    temp.name = host;
    rctx = ngx_resolve_start(clcf->resolver, &temp);
    if (rctx == NULL) {
        u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;
        lua_pushnil(L);
        lua_pushliteral(L, "failed to start the resolver");
        return 2;
    }

    if (rctx == NGX_NO_RESOLVER) {
        u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;
        lua_pushnil(L);
        lua_pushfstring(L, "no resolver defined to resolve \"%s\"", host.data);
        return 2;
    }

    rctx->name = host;
    rctx->type = NGX_RESOLVE_A;
    rctx->handler = ngx_http_lua_socket_resolve_handler;
    rctx->data = u;
    rctx->timeout = clcf->resolver_timeout;

    u->resolved->ctx = rctx;

    saved_top = lua_gettop(L);

    if (ngx_resolve_name(rctx) != NGX_OK) {
        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "lua udp socket fail to run resolver immediately");

        u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;

        u->resolved->ctx = NULL;
        lua_pushnil(L);
        lua_pushfstring(L, "%s could not be resolved", host.data);

        return 2;
    }

    if (u->waiting == 1) {
        /* resolved and already connecting */
        return lua_yield(L, 0);
    }

    n = lua_gettop(L) - saved_top;
    if (n) {
        /* errors occurred during resolving or connecting
         * or already connected */
        return n;
    }

    /* still resolving */

    u->waiting = 1;
    u->prepare_retvals = ngx_http_lua_socket_resolve_retval_handler;

    ctx->data = u;
    ctx->udp_socket_busy = 1;
    ctx->udp_socket_ready = 0;

    if (ctx->entered_content_phase) {
        r->write_event_handler = ngx_http_lua_content_wev_handler;
    }

    return lua_yield(L, 0);
}
Esempio n. 20
0
int LuaGlobalFunctions_bit_shiftright(lua_State * L)
{
	uint32 left = CHECK_ULONG(L,1);
	uint8 count = luaL_checkinteger(L,2) & 0x7F;
	RET_NUMBER(left >> count)
}
Esempio n. 21
0
/*
** Traversal function for 'ipairs' for tables with metamethods
*/
static int ipairsaux (lua_State *L) {
  lua_Integer i = luaL_checkinteger(L, 2) + 1;
  lua_pushinteger(L, i);
  return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
}
Esempio n. 22
0
static int str_format (lua_State *L) {
  int top = lua_gettop(L);
  int arg = 1;
  size_t sfl;
  const char *strfrmt = luaL_checklstring(L, arg, &sfl);
  const char *strfrmt_end = strfrmt+sfl;
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  while (strfrmt < strfrmt_end) {
    if (*strfrmt != L_ESC)
      luaL_addchar(&b, *strfrmt++);
    else if (*++strfrmt == L_ESC)
      luaL_addchar(&b, *strfrmt++);  /* %% */
    else { /* format item */
      char form[MAX_FORMAT];  /* to store the format (`%...') */
      char buff[MAX_ITEM];  /* to store the formatted item */
      if (++arg > top)
        luaL_argerror(L, arg, "no value");
      strfrmt = scanformat(L, strfrmt, form);
      switch (*strfrmt++) {
        case 'c': {
          sprintf(buff, form, (int)luaL_checknumber(L, arg));
          break;
        }
        case 'd':  case 'i': {
          addintlen(form);
#ifdef LUA_TINT
          sprintf(buff, form, (LUA_INTFRM_T)luaL_checkinteger(L, arg));
#else
          sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
#endif
          break;
        }
/* AK 22-Jul-06 -> Lua authors: 
   The ouxX case is identical to di case; should they be merged? */
        case 'o':  case 'u':  case 'x':  case 'X': {
          addintlen(form);
#ifdef LUA_TINT
          sprintf(buff, form, (LUA_INTFRM_T)luaL_checkinteger(L, arg));
#else
          sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
#endif
          break;
        }
        case 'e':  case 'E': case 'f':
        case 'g': case 'G': {
          sprintf(buff, form, (double)luaL_checknumber(L, arg));
          break;
        }
        case 'q': {
          addquoted(L, &b, arg);
          continue;  /* skip the 'addsize' at the end */
        }
        case 's': {
          size_t l;
          const char *s = luaL_checklstring(L, arg, &l);
          if (!strchr(form, '.') && l >= 100) {
            /* no precision and string is too long to be formatted;
               keep original string */
            lua_pushvalue(L, arg);
            luaL_addvalue(&b);
            continue;  /* skip the `addsize' at the end */
          }
          else {
            sprintf(buff, form, s);
            break;
          }
        }
        default: {  /* also treat cases `pnLlh' */
          return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "
                               LUA_QL("format"), *(strfrmt - 1));
        }
      }
      luaL_addlstring(&b, buff, strlen(buff));
    }
  }
  luaL_pushresult(&b);
  return 1;
}
static int
ngx_http_lua_ngx_req_init_body(lua_State *L)
{
    ngx_http_request_t          *r;
    int                          n;
    ngx_http_request_body_t     *rb;
    size_t                       size;
    lua_Integer                  num;
#if 1
    ngx_temp_file_t             *tf;
#endif
    ngx_http_core_loc_conf_t    *clcf;

    n = lua_gettop(L);

    if (n != 1 && n != 0) {
        return luaL_error(L, "expecting 0 or 1 argument but seen %d", n);
    }

    r = ngx_http_lua_get_req(L);
    if (r == NULL) {
        return luaL_error(L, "no request found");
    }

    ngx_http_lua_check_fake_request(L, r);

    if (r->discard_body) {
        return luaL_error(L, "request body already discarded asynchronously");
    }

    if (r->request_body == NULL) {
        return luaL_error(L, "request body not read yet");
    }

    if (n == 1) {
        num = luaL_checkinteger(L, 1);
        if (num <= 0) {
            return luaL_error(L, "bad size argument: %d", (int) num);
        }

        size = (size_t) num;

    } else {

        clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
        size = clcf->client_body_buffer_size;

        size += size >> 2;

        /* avoid allocating an unnecessary large buffer */
        if (size > (size_t) r->headers_in.content_length_n) {
            size = (size_t) r->headers_in.content_length_n;
        }
    }

    rb = r->request_body;

#if 1
    tf = rb->temp_file;

    if (tf) {
        if (tf->file.fd != NGX_INVALID_FILE) {

            dd("cleaning temp file %.*s", (int) tf->file.name.len,
               tf->file.name.data);

            ngx_http_lua_pool_cleanup_file(r->pool, tf->file.fd);

            ngx_memzero(tf, sizeof(ngx_temp_file_t));

            tf->file.fd = NGX_INVALID_FILE;

            dd("temp file cleaned: %.*s", (int) tf->file.name.len,
               tf->file.name.data);
        }

        rb->temp_file = NULL;
    }
#endif

    r->request_body_in_clean_file = 1;

    r->headers_in.content_length_n = 0;

    rb->buf = ngx_create_temp_buf(r->pool, size);
    if (rb->buf == NULL) {
        return luaL_error(L, "no memory");
    }

    rb->bufs = ngx_alloc_chain_link(r->pool);
    if (rb->bufs == NULL) {
        return luaL_error(L, "no memory");
    }

    rb->bufs->buf = rb->buf;
    rb->bufs->next = NULL;

    return 0;
}
Esempio n. 24
0
static int groupSetMargined(lua_State *L)
{
    assert(yalulCheckControl(L, 1, YALUL_GROUP_LIB));
    uiGroupSetMargined(CAST_ARG(1, Group), luaL_checkinteger(L, 2));
    RETURN_SELF;
}
Esempio n. 25
0
static int gprs_clear( lua_State* L )
{
uint32_t timeout = luaL_checkinteger(L, 1);
	timeout *= 1000000;
	return sendCommand(EOL, EOL, MSG_NOK, 2, timeout);
}
Esempio n. 26
0
 static int setCursorMode(lua_State* L)
 {
     int mode = luaL_checkinteger(L, 1);
     W_INPUT.setCursorMode((CursorMode) mode);
     return 0;
 }
Esempio n. 27
0
static int color_setDefault(lua_State *L) {
	color_default = luaL_checkinteger(L, 1);

	return 0;
}
Esempio n. 28
0
static int math_ult (lua_State *L) {
  lua_Integer a = luaL_checkinteger(L, 1);
  lua_Integer b = luaL_checkinteger(L, 2);
  lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
  return 1;
}
Esempio n. 29
0
File: fs.c Progetto: ifzz/LuaIO
/*local stat, err = fs.fstat(fd)*/
static int LuaIO_fs_fstat(lua_State* L) {
  int fd = luaL_checkinteger(L, 1);

  CREATE_REQ2();
  FS_CALL2(fstat, req, fd);
}
Esempio n. 30
0
static int str_format (lua_State *L) {
  int top = lua_gettop(L);
  int arg = 1;
  size_t sfl;
  const char *strfrmt = luaL_checklstring(L, arg, &sfl);
  const char *strfrmt_end = strfrmt+sfl;
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  while (strfrmt < strfrmt_end) {
    if (*strfrmt != L_ESC)
      luaL_addchar(&b, *strfrmt++);
    else if (*++strfrmt == L_ESC)
      luaL_addchar(&b, *strfrmt++);  /* %% */
    else { /* format item */
      char form[MAX_FORMAT];  /* to store the format ('%...') */
      char *buff = luaL_prepbuffsize(&b, MAX_ITEM);  /* to put formatted item */
      int nb = 0;  /* number of bytes in added item */
      if (++arg > top)
        luaL_argerror(L, arg, "no value");
      strfrmt = scanformat(L, strfrmt, form);
      switch (*strfrmt++) {
        case 'c': {
          nb = c_sprintf(buff, form, (int)luaL_checkinteger(L, arg));
          break;
        }
        case 'd': case 'i':
        case 'o': case 'u': case 'x': case 'X': {
          lua_Integer n = luaL_checkinteger(L, arg);
          addlenmod(form, LUA_INTEGER_FRMLEN);
          nb = c_sprintf(buff, form, n);
          break;
        }
#if defined(LUA_USE_AFORMAT)
        case 'a': case 'A':
#endif
        case 'e': case 'E': case 'f':
        case 'g': case 'G': {
          addlenmod(form, LUA_NUMBER_FRMLEN);
          nb = c_sprintf(buff, form, luaL_checknumber(L, arg));
          break;
        }
        case 'q': {
          addquoted(L, &b, arg);
          break;
        }
        case 's': {
          size_t l;
          const char *s = luaL_tolstring(L, arg, &l);
          if (!c_strchr(form, '.') && l >= 100) {
            /* no precision and string is too long to be formatted;
               keep original string */
            luaL_addvalue(&b);
            break;
          }
          else {
            nb = c_sprintf(buff, form, s);
            lua_pop(L, 1);  /* remove result from 'luaL_tolstring' */
            break;
          }
        }
        default: {  /* also treat cases 'pnLlh' */
          return luaL_error(L, "invalid option '%%%c' to 'format'",
                               *(strfrmt - 1));
        }
      }
      luaL_addsize(&b, nb);
    }
  }
  luaL_pushresult(&b);
  return 1;
}