/* ** Unlocks a file. ** @param #1 File handle. ** @param #2 Number with start position (optional). ** @param #3 Number with length (optional). */ static int file_unlock (lua_State *L) { FILE *fh = check_file (L, 1, "unlock"); const long start = luaL_optlong (L, 2, 0); long len = luaL_optlong (L, 3, 0); if (_file_lock (L, fh, "u", start, len, "unlock")) { lua_pushboolean (L, 1); return 1; } else { lua_pushnil (L); lua_pushfstring (L, "%s", strerror(errno)); return 2; } }
/* * fs.read */ static int fs_read(lua_State* L) { FSR__SETUP int fd = luaL_checkint(L, 1); MemSlice *ms = lev_checkbuffer(L, 2); int64_t file_pos = luaL_optlong(L, 3, 0); file_pos--; /* Luaisms */ size_t file_until = luaL_optlong(L, 4, 0); if (file_until > ms->until) { file_until = ms->until; } FSR__SET_OPT_CB(5, on_fs_callback) uv_fs_read(loop, req, fd, ms->slice, (file_until ? file_until : ms->until), file_pos, cb); FSR__TEARDOWN }
LDLUA_METHOD meta_add_uint(lua_State* L) { int32_t status; #define LDLUA_OPTARG_META_TYPE 2 #define LDLUA_OPTARG_NUM 3 meta ss = check_meta(L, 1); uint16_t type = (uint16_t)luaL_optlong(L, LDLUA_OPTARG_META_TYPE, 0); uint32_t num = (uint32_t)luaL_optlong(L, LDLUA_OPTARG_NUM, 0); status = meta_buffer_item_add(ss, type, &num, sizeof(uint32_t)); if (status != 0) { luaL_error(L,"savestr error, status %d\n", status); } return 0; }
static int luaP_cursormove (lua_State *L) { luaP_Cursor *c = (luaP_Cursor *) luaP_checkudata(L, 1, PLLUA_CURSORMT); luaP_TRY { SPI_cursor_move(c->cursor, 1, luaL_optlong(L, 2, 0)); } luaP_CATCH; return 0; }
static int Lnew(lua_State *L) /** new([seed]) */ { long seed=luaL_optlong(L,1,SEED); MT *c=Pnew(L); init_genrand(c,seed); return 1; }
// Lua: net.createServer(type, timeout) int net_createServer( lua_State *L ) { int type, timeout; type = luaL_optlong(L, 1, TYPE_TCP); timeout = luaL_optlong(L, 2, 30); if (type == TYPE_UDP) { platform_print_deprecation_note("net.createServer with net.UDP type", "in next version"); return net_createUDPSocket( L ); } if (type != TYPE_TCP) return luaL_error(L, "invalid type"); lnet_userdata *u = net_create(L, TYPE_TCP_SERVER); u->server.timeout = timeout; return 1; }
static int luaV_list_insert (lua_State *L) { luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST); list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis); long pos = luaL_optlong(L, 3, 0); listitem_T *li = NULL; typval_T v; if (l->lv_lock) luaL_error(L, "list is locked"); if (pos < l->lv_len) { li = list_find(l, pos); if (li == NULL) luaL_error(L, "invalid position"); } lua_settop(L, 2); luaV_totypval(L, 2, &v); if (list_insert_tv(l, &v, li) == FAIL) { clear_tv(&v); luaL_error(L, "Failed to add item to list"); } clear_tv(&v); lua_settop(L, 1); return 1; }
static int shm_seek(lua_State *L) { const char *const modes[] = { "set", "cur", "end", NULL }; lua_apr_shm *object; size_t offset; int mode; object = check_shm(L, 1); mode = luaL_checkoption(L, 2, "cur", modes); offset = luaL_optlong(L, 3, 0); if (mode == 1) { /* CUR */ offset += object->last_op->index; } else if (mode == 2) { /* END */ offset += object->last_op->limit - 1; } luaL_argcheck(L, offset >= 0, 2, "cannot seek before start of shared memory segment!"); luaL_argcheck(L, offset < object->size, 2, "cannot seek past end of shared memory segment!"); object->input.buffer.index = offset; object->output.buffer.index = offset; lua_pushinteger(L, offset); return 1; }
static int lua__lcs_read(lua_State *L) { char tmp[8192]; char *buf = (char *)&tmp; ssize_t rsz = 0; sock_client_t * client = CHECK_CLIENT(L, 1); size_t sz = luaL_optlong(L, 2, sizeof(tmp)); if (!client->connected) { return luaL_error(L, "not connected"); } if (!lcs_fdcanread(client->fd)) { lua_pushboolean(L, 0); lua_pushstring(L, "no data"); return 2; } if (sz > sizeof(tmp)) { buf = malloc(sz); if (buf == NULL) { return luaL_error(L, "nomem while read"); } } rsz = recv(client->fd, buf, sz, 0); if (rsz > 0) { lua_pushlstring(L, buf, rsz); } else if (rsz <= 0) { client->connected = 0; } if (buf != (char *)&tmp) { free(buf); } return rsz > 0 ? 1 : 0; }
static int nixio__gso_mreq6(lua_State *L, int fd, int level, int opt, int set) { struct ipv6_mreq val; socklen_t optlen = sizeof(val); if (!set) { char buf[INET_ADDRSTRLEN]; if (!getsockopt(fd, level, opt, (char *)&val, &optlen)) { if (!inet_ntop(AF_INET6, &val.ipv6mr_multiaddr, buf, sizeof(buf))) { return nixio__perror_s(L); } lua_pushstring(L, buf); lua_pushinteger(L, val.ipv6mr_interface); return 2; } } else { const char *maddr = luaL_checkstring(L, set); if (inet_pton(AF_INET6, maddr, &val.ipv6mr_multiaddr) < 1) { return nixio__perror_s(L); } val.ipv6mr_interface = luaL_optlong(L, set + 1, 0); if (!setsockopt(fd, level, opt, (char *)&val, optlen)) { lua_pushboolean(L, 1); return 1; } } return nixio__perror_s(L); }
static int str_byte (lua_State *L) { size_t l; const lua_WChar *s = luaL_checklwstring(L, 1, &l); sint32 pos = posrelat(luaL_optlong(L, 2, 1), l); luaL_argcheck(L, 0 < pos && (size_t)(pos) <= l, 2, "out of range"); lua_pushnumber(L, s[pos-1]); return 1; }
static int luaP_cursorposmove (lua_State *L) { luaP_Cursor *c = (luaP_Cursor *) luaP_checkudata(L, 1, PLLUA_CURSORMT); FetchDirection fd = (lua_toboolean(L, 3)) ? FETCH_RELATIVE : FETCH_ABSOLUTE; luaP_TRY { SPI_scroll_cursor_move(c->cursor, fd, luaL_optlong(L, 2, 0)); } luaP_CATCH; return 0; }
static int str_byte (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 1, &l); sint32 pos = posrelat(luaL_optlong(L, 2, 1), l); if (pos <= 0 || (size_t)(pos) > l) /* index out of range? */ return 0; /* no answer */ lua_pushnumber(L, uchar(s[pos-1])); return 1; }
LDLUA_METHOD meta_add_ff(lua_State* L) { int32_t status; meta_ff_t ff; #define LDLUA_OPTARG_IP 2 #define LDLUA_OPTARG_PORT 3 #define LDLUA_OPTARG_APP_TYPE 4 meta ss = check_meta(L, 1); ff.ip = (uint32_t)luaL_optlong(L, LDLUA_OPTARG_IP, 0); ff.port = (uint16_t)luaL_optlong(L, LDLUA_OPTARG_PORT, 0); ff.app_type = (uint32_t)luaL_optlong(L, LDLUA_OPTARG_APP_TYPE, 0); status = meta_buffer_item_add(ss, META_TYPE_FF, &ff, sizeof(ff)); if (status != 0) { luaL_error(L,"savestr error, status %d\n", status); } return 0; }
static int luaP_cursorfetch (lua_State *L) { luaP_Cursor *c = (luaP_Cursor *) luaP_checkudata(L, 1, PLLUA_CURSORMT); luaP_TRY { SPI_cursor_fetch(c->cursor, 1, luaL_optlong(L, 2, FETCH_ALL)); } luaP_CATCH; if (SPI_processed > 0) /* any rows? */ luaP_pushtuptable(L, c->cursor); else lua_pushnil(L); return 1; }
static int file_truncate(lua_State *L) { apr_status_t status; lua_apr_file *file; apr_off_t offset; file = file_check(L, 1, 1); offset = luaL_optlong(L, 2, 0); status = apr_file_trunc(file->handle, offset); return push_status(L, status); }
static int str_sub (lua_State *L) { size_t l; const lua_WChar *s = luaL_checklwstring(L, 1, &l); sint32 start = posrelat(luaL_checklong(L, 2), l); sint32 end = posrelat(luaL_optlong(L, 3, -1), l); if (start < 1) start = 1; if (end > (sint32)l) end = l; if (start <= end) lua_pushlwstring(L, s+start-1, end-start+1); else lua_pushwliteral(L, L""); return 1; }
static int luaP_cursorposfetch (lua_State *L) { luaP_Cursor *c = (luaP_Cursor *) luaP_checkudata(L, 1, PLLUA_CURSORMT); FetchDirection fd = (lua_toboolean(L, 3)) ? FETCH_RELATIVE : FETCH_ABSOLUTE; luaP_TRY { SPI_scroll_cursor_fetch(c->cursor, fd, luaL_optlong(L, 2, FETCH_ALL)); } luaP_CATCH; if (SPI_processed > 0) /* any rows? */ luaP_pushtuptable(L, c->cursor); else lua_pushnil(L); return 1; }
static int file_seek(lua_State *L) { /* TODO Seek the write buffer as well! */ const char *const modenames[] = { "set", "cur", "end", NULL }; const apr_seek_where_t modes[] = { APR_SET, APR_CUR, APR_END }; apr_status_t status; lua_apr_file *file; lua_apr_buffer *B; apr_off_t offset; int mode; file = file_check(L, 1, 1); B = &file->input.buffer; mode = modes[luaL_checkoption(L, 2, "cur", modenames)]; offset = luaL_optlong(L, 3, 0); /* XXX Flush write buffer before changing offset! */ if (!(mode == APR_CUR && offset == 0)) { status = flush_buffer(L, &file->output, 1); if (status != APR_SUCCESS) return push_file_error(L, file, status); } /* Make relative offsets absolute, adjust for buffered input. */ if (mode == APR_CUR && B->index < B->limit) { apr_off_t temp = 0; status = apr_file_seek(file->handle, APR_CUR, &temp); if (status != APR_SUCCESS) return push_file_error(L, file, status); mode = APR_SET, offset = temp - (B->limit - B->index); } /* Perform the requested seek() operation. */ status = apr_file_seek(file->handle, mode, &offset); if (status != APR_SUCCESS) return push_file_error(L, file, status); /* Invalidate all buffered input (very inefficient but foolproof: parts of * the buffer may have been modified by the binary to text translation). * XXX The write buffer has already been reset by flush_buffer() above. * FIXME Don't invalidate the buffered input unnecessarily?! */ file->input.buffer.index = 0; file->input.buffer.limit = 0; /* FIXME Bound to lose precision when APR_FOPEN_LARGEFILE is in effect? */ lua_pushnumber(L, (lua_Number) offset); return 1; }
// Lua: net.createConnection(type, secure) int net_createConnection( lua_State *L ) { int type, secure; type = luaL_optlong(L, 1, TYPE_TCP); secure = luaL_optlong(L, 2, 0); if (type == TYPE_UDP) { platform_print_deprecation_note("net.createConnection with net.UDP type", "in next version"); return net_createUDPSocket( L ); } if (type != TYPE_TCP) return luaL_error(L, "invalid type"); if (secure) { platform_print_deprecation_note("net.createConnection with secure flag", "in next version"); #ifdef TLS_MODULE_PRESENT return tls_socket_create( L ); #else return luaL_error(L, "secure connections not enabled"); #endif } net_create(L, TYPE_TCP_CLIENT); return 1; }
static int luaP_executeplan (lua_State *L) { luaP_Plan *p = (luaP_Plan *) luaP_checkudata(L, 1, PLLUA_PLANMT); bool ro = (bool) lua_toboolean(L, 3); long c = luaL_optlong(L, 4, 0); int result = -1; if (p->nargs > 0) { luaP_Buffer *b; if (lua_type(L, 2) != LUA_TTABLE) luaP_typeerror(L, 2, "table"); b = luaP_getbuffer(L, p->nargs); luaP_fillbuffer(L, 2, p->type, b); luaP_TRY { result = SPI_execute_plan(p->plan, b->value, b->null, ro, c); } luaP_CATCH; }
static int f_seek (lua_State *L) { static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; FILE *f = tofile(L); int op = luaL_checkoption(L, 2, "cur", modenames); long offset = luaL_optlong(L, 3, 0); op = fseek(f, offset, mode[op]); if (op) return pushresult(L, 0, NULL); /* error */ else { lua_pushinteger(L, ftell(f)); return 1; } }
static int f_seek (lua_State *L) { static const int mode[] = {SEEK_SET, SEEK_CUR}; static const char *const modenames[] = {"set", "cur", NULL}; gzFile f = tofile(L, 1); int op = luaL_checkoption(L, 2, "cur", modenames); long offset = luaL_optlong(L, 3, 0); op = gzseek(f, offset, mode[op]); if (op == -1) return pushresult(L, 0, NULL); /* error */ else { lua_pushnumber(L, op); return 1; } }
static int f_seek (lua_State *L) { static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; FILE *f = tofile(L, 1); int op = luaL_findstring(luaL_optstring(L, 2, "cur"), modenames); long offset = luaL_optlong(L, 3, 0); luaL_argcheck(L, op != -1, 2, "invalid mode"); op = fseek(f, offset, mode[op]); if (op) return pushresult(L, 0, NULL); /* error */ else { lua_pushnumber(L, ftell(f)); return 1; } }
static int ff_seek (lua_State *L) { static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; ZZIP_FILE *f = tointernalfile(L, 1); int op = luaL_checkoption(L, 2, "cur", modenames); long offset = luaL_optlong(L, 3, 0); luaL_argcheck(L, op != -1, 2, "invalid mode"); op = zzip_seek(f, offset, mode[op]); if (op < 0) return pushresult(L, 0, NULL); /* error */ else { lua_pushnumber(L, zzip_tell(f)); return 1; } }
static int file_seek (lua_State *L) { static const int mode[] = {FS_SEEK_SET, FS_SEEK_CUR, FS_SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; if((FS_OPEN_OK - 1)==file_fd) return luaL_error(L, "open a file first"); int op = luaL_checkoption(L, 1, "cur", modenames); long offset = luaL_optlong(L, 2, 0); op = fs_seek(file_fd, offset, mode[op]); if (op) lua_pushnil(L); /* error */ else lua_pushinteger(L, fs_tell(file_fd)); return 1; }
int lua_apr_thread_queue(lua_State *L) { apr_status_t status; lua_apr_queue *object; unsigned int capacity; capacity = luaL_optlong(L, 1, 1); luaL_argcheck(L, capacity >= 1, 1, "capacity must be >= 1"); object = new_object(L, &lua_apr_queue_type); status = apr_pool_create(&object->pool, NULL); if (status == APR_SUCCESS) status = apr_queue_create(&object->handle, capacity, object->pool); if (status != APR_SUCCESS) return push_error_status(L, status); return 1; }
//file.seek(whence, offset) static int file_seek (lua_State *L) { static const int mode[] = {SPIFFS_SEEK_SET, SPIFFS_SEEK_CUR, SPIFFS_SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; if(FILE_NOT_OPENED==file_fd) return luaL_error(L, "open a file first"); int op = luaL_checkoption(L, 1, "cur", modenames); long offset = luaL_optlong(L, 2, 0); op = SPIFFS_lseek(&fs,file_fd, offset, mode[op]); if (op) lua_pushnil(L); /* error */ else { spiffs_fd *fd; spiffs_fd_get(&fs, file_fd, &fd); lua_pushinteger(L, fd->fdoffset); } return 1; }
WSLUA_METHOD File_seek(lua_State* L) { /* Seeks in the File, similar to Lua's `file:seek()`. See Lua 5.x ref manual for `file:seek()`. */ static const int mode[] = { SEEK_SET, SEEK_CUR, SEEK_END }; static const char *const modenames[] = {"set", "cur", "end", NULL}; File f = checkFile(L,1); int op = luaL_checkoption(L, 2, "cur", modenames); gint64 offset = (gint64) luaL_optlong(L, 3, 0); int err = WTAP_ERR_INTERNAL; if (file_is_reader(f)) { offset = file_seek(f->file, offset, mode[op], &err); if (offset < 0) { lua_pushnil(L); /* error */ lua_pushstring(L, wtap_strerror(err)); return 2; } lua_pushnumber(L, (lua_Number)(file_tell(f->file))); } else { offset = wtap_dump_file_seek(f->wdh, offset, mode[op], &err); if (offset < 0) { lua_pushnil(L); /* error */ lua_pushstring(L, wtap_strerror(err)); return 2; } offset = wtap_dump_file_tell(f->wdh, &err); if (offset < 0) { lua_pushnil(L); /* error */ lua_pushstring(L, wtap_strerror(err)); return 2; } lua_pushnumber(L, (lua_Number)(offset)); } WSLUA_RETURN(1); /* The current file cursor position as a number. */ }
static int str_find(lua_State * L) { size_t l1, l2; const char *s = luaL_checklstring(L, 1, &l1); const char *p = luaL_checklstring(L, 2, &l2); sint32 init = posrelat(luaL_optlong(L, 3, 1), l1) - 1; if (init < 0) init = 0; else if ((size_t) (init) > l1) init = (sint32) l1; if (lua_toboolean(L, 4) || /* explicit request? */ strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */ /* do a plain search */ const char *s2 = lmemfind(s + init, l1 - init, p, l2); if (s2) { lua_pushnumber(L, (lua_Number) (s2 - s + 1)); lua_pushnumber(L, (lua_Number) (s2 - s + l2)); return 2; } } else { MatchState ms; int anchor = (*p == '^') ? (p++, 1) : 0; const char *s1 = s + init; ms.L = L; ms.src_init = s; ms.src_end = s + l1; do { const char *res; ms.level = 0; if ((res = match(&ms, s1, p)) != NULL) { lua_pushnumber(L, (lua_Number) (s1 - s + 1)); /* start */ lua_pushnumber(L, (lua_Number) (res - s)); /* end */ return push_captures(&ms, NULL, 0) + 2; } } while (s1++ < ms.src_end && !anchor); } lua_pushnil(L); /* not found */ return 1; }