static int lua_kpathsea_show_path(lua_State * L) { kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE); int op = luaL_checkoption(L, -1, "tex", filetypenames); unsigned user_format = filetypes[op]; if (!(*kp)->format_info[user_format].type) /* needed if arg was numeric */ kpathsea_init_format(*kp, user_format); lua_pushstring(L, (*kp)->format_info[user_format].path); return 1; }
static int f_setvbuf(lua_State* L) { static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; static const char* const modenames[] = {"no", "full", "line", NULL}; FILE* f = tofile(L); int op = luaL_checkoption(L, 2, NULL, modenames); lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); int res = setvbuf(f, NULL, mode[op], sz); return pushresult(L, res == 0, NULL); }
static int show_path(lua_State * L) { int op = luaL_checkoption(L, -1, "tex", filetypenames); unsigned user_format = filetypes[op]; TEST_PROGRAM_NAME_SET; if (!kpse_format_info[user_format].type) /* needed if arg was numeric */ kpse_init_format(user_format); lua_pushstring(L, kpse_format_info[user_format].path); return 1; }
int lc_syslog_log(lua_State* L) { int level = luaL_checkoption(L, 1, "notice", level_strings); level = level_constants[level]; luaL_checkstring(L, 2); syslog(level, "%s", lua_tostring(L, 2)); return 0; }
static int os_setlocale (lua_State *L) { static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME}; static const char *const catnames[] = {"all", "collate", "ctype", "monetary", "numeric", "time", NULL}; const char *l = luaL_optstring(L, 1, NULL); int op = luaL_checkoption(L, 2, "all", catnames); lua_pushstring(L, setlocale(cat[op], l)); return 1; }
/* host, port = socket:getaddress([site]) */ static int sck_getaddress (lua_State *L) { static const char *const sites[] = {"local", "remote", NULL}; loski_NetDriver *drv = (loski_NetDriver *)lua_touserdata(L, lua_upvalueindex(1)); loski_Socket *socket = tosock(L, LOSKI_BASESOCKET); loski_Address address; int site = luaL_checkoption(L, 2, "local", sites); int res = loski_socketaddress(drv, socket, &address, site); if (res == 0) return pushaddress(drv, L, &address); return pushsockres(L, 0, res); }
static int camera_get(lua_State *L) { camera_t *cam = *((camera_t **)luaL_checkudata(L, 1, META_CAMERA)); enum cameraf field = luaL_checkoption(L, 2, NULL, camera_opt); // cameras should always be valid unless I'm a nutter I_Assert(cam != NULL); switch (field) { case camera_chase: lua_pushboolean(L, cam->chase); break; case camera_aiming: lua_pushinteger(L, cam->aiming); break; case camera_x: lua_pushinteger(L, cam->x); break; case camera_y: lua_pushinteger(L, cam->y); break; case camera_z: lua_pushinteger(L, cam->z); break; case camera_angle: lua_pushinteger(L, cam->angle); break; case camera_subsector: LUA_PushUserdata(L, cam->subsector, META_SUBSECTOR); break; case camera_floorz: lua_pushinteger(L, cam->floorz); break; case camera_ceilingz: lua_pushinteger(L, cam->ceilingz); break; case camera_radius: lua_pushinteger(L, cam->radius); break; case camera_height: lua_pushinteger(L, cam->height); break; case camera_momx: lua_pushinteger(L, cam->momx); break; case camera_momy: lua_pushinteger(L, cam->momy); break; case camera_momz: lua_pushinteger(L, cam->momz); break; } return 1; }
static int cb( struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfqdata, void *data ) { /* TODO - should have an option "delay", to explicitly avoid offering a verdict right away */ static const char* verdict_opt[] = { "accept", "drop", NULL }; static int verdict_val[] = { NF_ACCEPT, NF_DROP, }; lua_State* L = data; struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfqdata); /* struct nfqnl_msg_packet_hw *hwph = nfq_get_msg_packet_hw(nfdata); */ u_int32_t id = 0; if (ph) { /* TODO - why is this conditional in sample source? */ id = ntohl(ph->packet_id); } /* We expect stack to look like: * [1] qhandle * [2] cbfn */ check_handle(L); luaL_checktype(L, 2, LUA_TFUNCTION); lua_pushvalue(L, 2); /* Push copy of fn */ lua_pushlightuserdata(L, nfqdata); lua_call(L, 1, 2); /* Return will be: * [3] "accept", "drop", ..., default is accept * [4] string, the replacement packet, default is 0,NULL */ { int verdict = luaL_checkoption(L, 3, "accept", verdict_opt); size_t replacesz = 0; const char* replace = lua_tolstring(L, 4, &replacesz); /*printf("verdict %s data %p data_len %zd\n", verdict_opt[verdict], replace, replacesz);*/ /* Reset stack, chopping any return values. */ lua_settop(L, 2); return nfq_set_verdict(qh, id, verdict_val[verdict], replacesz, (void*)replace); } }
/* * old_handler[, err] == signal(signal [, func]) * * signal = signal number or string * func/"ignore"/"default" = Lua function to call */ static int l_signal (lua_State *L) { enum {IGNORE, DEFAULT, SET}; static const char *options[] = {"ignore", "default", NULL}; int sig = get_signal(L, 1); int option; if (lua_isstring(L, 2)) option = luaL_checkoption(L, 2, NULL, options); else if (lua_isnil(L, 2)) option = DEFAULT; else option = (luaL_checktype(L, 2, LUA_TFUNCTION), SET); lua_pushvalue(L, 1); lua_rawget(L, LUA_ENVIRONINDEX); /* return old handler */ lua_pushvalue(L, 1); switch (option) { case IGNORE: lua_pushnil(L); lua_rawset(L, LUA_ENVIRONINDEX); signal(sig, SIG_IGN); signal_stack[sig+signal_stack_top] = signal_stack[sig] = 0; break; case DEFAULT: lua_pushnil(L); lua_rawset(L, LUA_ENVIRONINDEX); signal(sig, SIG_DFL); signal_stack[sig+signal_stack_top] = signal_stack[sig] = 0; break; case SET: lua_pushvalue(L, 2); lua_rawset(L, LUA_ENVIRONINDEX); #if USE_SIGACTION { struct sigaction act; act.sa_handler = handle; sigemptyset(&act.sa_mask); act.sa_flags = 0; if (sigaction(sig, &act, NULL)) return status(L, 0); } #else if (signal(sig, handle) == SIG_ERR) return status(L, 0); #endif break; default: assert(0); } return 1; }
int lc_syslog_setmask(lua_State* L) { int level_idx = luaL_checkoption(L, 1, "notice", level_strings); int mask = 0; do { mask |= LOG_MASK(level_constants[level_idx]); } while(++level_idx <= 4); setlogmask(mask); return 0; }
int lc_syslog_log(lua_State* L) { int level = level_constants[luaL_checkoption(L, 1, "notice", level_strings)]; if(lua_gettop(L) == 3) { syslog(level, "%s: %s", luaL_checkstring(L, 2), luaL_checkstring(L, 3)); } else { syslog(level, "%s", lua_tostring(L, 2)); } return 0; }
static int io_setloc (lua_State *L) { static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME}; static const char *const catnames[] = {"all", "collate", "ctype", "monetary", "numeric", "time", NULL}; const char *l = lua_tostring(L, 1); int op = luaL_checkoption(L, 2, "all", catnames); luaL_argcheck(L, l || lua_isnoneornil(L, 1), 1, "string expected"); lua_pushstring(L, setlocale(cat[op], l)); return 1; }
static int lzstream_flush(lua_State *L) { static int flush_values[] = { Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH }; static const char *const flush_opts[] = { "sync", "full", "finish" }; lz_stream *s = lzstream_check(L, 1, LZ_DEFLATE); int flush = luaL_checkoption(L, 2, flush_opts[0], flush_opts); lua_settop(L, 0); lua_pushliteral(L, ""); return lzstream_docompress(L, s, 1, 1, flush_values[flush]); }
static int f_setvbuf(lua_State * L) { int sz, op, res; static const int mode[] = { _IONBF, _IOFBF, _IOLBF }; static const char *const modenames[] = { "no", "full", "line", NULL }; FILE *f = tofile(L); op = luaL_checkoption(L, 2, NULL, modenames); lua_number2int(sz, luaL_optinteger(L, 3, LUAL_BUFFERSIZE)); res = setvbuf(f, NULL, mode[op], (size_t) sz); return pushresult(L, res == 0, NULL); }
/* * Arguments: evq_udata, signal (string), callback (function), * [timeout (milliseconds), one_shot (boolean)] * Returns: [ev_ludata] */ static int levq_add_signal (lua_State *L) { const int signo = sig_flags[luaL_checkoption(L, 2, NULL, sig_names)]; lua_settop(L, 5); lua_pushinteger(L, signo); /* signal */ lua_replace(L, 2); lua_pushinteger(L, EVENT_READ | EVENT_SIGNAL); /* event_flags */ lua_insert(L, 3); return levq_add(L); }
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; }
/* * Arguments: evq_udata, signal (string), ignore (boolean) * Returns: [evq_udata] */ static int levq_ignore_signal (lua_State *L) { struct event_queue *evq = checkudata(L, 1, EVQ_TYPENAME); const int signo = sig_flags[luaL_checkoption(L, 2, NULL, sig_names)]; const int ignore = lua_toboolean(L, 3); if (!evq_ignore_signal(evq, signo, ignore)) { lua_settop(L, 1); return 1; } return sys_seterror(L, 0); }
/* * Arguments: evq_udata, [signal (string)] * Returns: [evq_udata] */ static int levq_signal (lua_State *L) { struct event_queue *evq = checkudata(L, 1, EVQ_TYPENAME); const int signo = lua_isnoneornil(L, 2) ? EVQ_SIGEVQ : sig_flags[luaL_checkoption(L, 2, NULL, sig_names)]; if (!evq_signal(evq, signo)) { lua_settop(L, 1); return 1; } return sys_seterror(L, 0); }
/** Register a new xproperty. * \param L The Lua VM state. * \return The number of elements pushed on stack. * \luastack * \lparam The name of the X11 property * \lparam One of "string", "number" or "boolean" */ int luaA_register_xproperty(lua_State *L) { const char *name; struct xproperty property; struct xproperty *found; const char *const args[] = { "string", "number", "boolean" }; xcb_intern_atom_reply_t *atom_r; int type; name = luaL_checkstring(L, 1); type = luaL_checkoption(L, 2, NULL, args); if (type == 0) property.type = PROP_STRING; else if (type == 1) property.type = PROP_NUMBER; else property.type = PROP_BOOLEAN; atom_r = xcb_intern_atom_reply(globalconf.connection, xcb_intern_atom_unchecked(globalconf.connection, false, a_strlen(name), name), NULL); if(!atom_r) return 0; property.atom = atom_r->atom; p_delete(&atom_r); found = xproperty_array_lookup(&globalconf.xproperties, &property); if(found) { /* Property already registered */ if(found->type != property.type) return luaL_error(L, "xproperty '%s' already registered with different type", name); } else { buffer_t buf; buffer_inita(&buf, a_strlen(name) + a_strlen("xproperty::") + 1); buffer_addf(&buf, "xproperty::%s", name); property.name = a_strdup(name); xproperty_array_insert(&globalconf.xproperties, property); signal_add(&window_class.signals, buf.s); signal_add(&global_signals, buf.s); buffer_wipe(&buf); } return 0; }
static int lmz_inflator_deflator_impl(lua_State* L, lmz_stream_t* stream) { mz_streamp miniz_stream = &(stream->stream); size_t data_size; const char* data = luaL_checklstring(L, 2, &data_size); int flush = luaL_checkoption(L, 3, "no", flush_types); miniz_stream->avail_in = data_size; miniz_stream->next_in = (const unsigned char*)data; luaL_Buffer buf; luaL_buffinit(L, &buf); while (1) { char* buffer = luaL_prepbuffer(&buf); memset(buffer, 0, LUAL_BUFFERSIZE); miniz_stream->avail_out = LUAL_BUFFERSIZE; miniz_stream->next_out = (unsigned char*)buffer; size_t before = miniz_stream->total_out; int status; if (stream->mode) { status = mz_inflate(miniz_stream, flush); } else { status = mz_deflate(miniz_stream, flush); } size_t added = miniz_stream->total_out - before; luaL_addsize(&buf, added); switch (status) { case MZ_OK: case MZ_STREAM_END: luaL_pushresult(&buf); return 1; case MZ_STREAM_ERROR: case MZ_DATA_ERROR: case MZ_PARAM_ERROR: luaL_pushresult(&buf); lua_pushnil(L); lua_insert(L, -2); lua_pushstring(L, mz_error(status)); lua_insert(L, -2); return 3; case MZ_BUF_ERROR: if (stream->mode) { // not enough input luaL_pushresult(&buf); lua_pushnil(L); lua_insert(L, -2); lua_pushstring(L, "Not enough input data"); lua_insert(L, -2); return 3; } break; } } }
static int luv_check_open_flags(lua_State *L, int idx, const char *def){ static const char *names[] = { "r" , "rs" , "sr" , "r+" , "rs+", "sr+", "w" , "wx" , "xw" , "w+" , "wx+", "xw+", "a" , "ax" , "xa" , "a+" , "ax+", "xa+", NULL, }; static const int flags[] = { O_RDONLY ,/* r */ O_RDONLY | O_SYNC ,/* rs */ O_RDONLY | O_SYNC ,/* sr */ O_RDWR ,/* r+ */ O_RDWR | O_SYNC ,/* rs+ */ O_RDWR | O_SYNC ,/* sr+ */ O_TRUNC | O_CREAT | O_WRONLY ,/* w */ O_TRUNC | O_CREAT | O_WRONLY | O_EXCL ,/* wx */ O_TRUNC | O_CREAT | O_WRONLY | O_EXCL ,/* xw */ O_TRUNC | O_CREAT | O_RDWR ,/* w+ */ O_TRUNC | O_CREAT | O_RDWR | O_EXCL ,/* wx+ */ O_TRUNC | O_CREAT | O_RDWR | O_EXCL ,/* xw+ */ O_APPEND | O_CREAT | O_WRONLY ,/* a */ O_APPEND | O_CREAT | O_WRONLY | O_EXCL ,/* ax */ O_APPEND | O_CREAT | O_WRONLY | O_EXCL ,/* xa */ O_APPEND | O_CREAT | O_RDWR ,/* a+ */ O_APPEND | O_CREAT | O_RDWR | O_EXCL ,/* ax+ */ O_APPEND | O_CREAT | O_RDWR | O_EXCL ,/* xa+ */ }; //! @todo static assert before change names/flags int flag = luaL_checkoption(L, idx, def, names); return flags[flag]; }
static int xmlreader_get_parser_property(lua_State *L) { xmlreader xr = check_xmlreader(L, 1); int prop = luaL_checkoption(L, 2, NULL, xmlreader_properties); int ret = xmlTextReaderGetParserProp(xr, prop); if (ret != -1) { lua_pushinteger(L, ret); return 1; } else { lua_pushnil(L); xmlreader_pusherror(L); return 2; } }
/* const char* interface_addr, uv_membership membership);*/ int luv_udp_set_membership(lua_State* L) { uv_udp_t* handle = (uv_udp_t*)luv_checkudata(L, 1, "udp"); const char* multicast_addr = luaL_checkstring(L, 2); const char* interface_addr = luaL_checkstring(L, 3); int option = luaL_checkoption (L, 4, "membership", luv_membership_options); uv_membership membership = option ? UV_LEAVE_GROUP : UV_JOIN_GROUP; if (uv_udp_set_membership(handle, multicast_addr, interface_addr, membership)) { uv_err_t err = uv_last_error(luv_get_loop(L)); return luaL_error(L, "udp_set_membership: %s", uv_strerror(err)); } return 0; }
/* * Arguments: sd_udata, option (string), * [value_lo (number), value_hi (number)] * Returns: [sd_udata | value_lo (number), value_hi (number)] */ static int sock_sockopt (lua_State *L) { static const int opt_flags[] = { SO_REUSEADDR, SO_TYPE, SO_ERROR, SO_DONTROUTE, SO_SNDBUF, SO_RCVBUF, SO_SNDLOWAT, SO_RCVLOWAT, SO_BROADCAST, SO_KEEPALIVE, SO_OOBINLINE, SO_LINGER, #define OPTNAMES_TCP 12 TCP_NODELAY, #define OPTNAMES_IP 13 IP_MULTICAST_TTL, IP_MULTICAST_IF, IP_MULTICAST_LOOP }; static const char *const opt_names[] = { "reuseaddr", "type", "error", "dontroute", "sndbuf", "rcvbuf", "sndlowat", "rcvlowat", "broadcast", "keepalive", "oobinline", "linger", "tcp_nodelay", "multicast_ttl", "multicast_if", "multicast_loop", NULL }; #undef OPT_START #define OPT_START 2 sd_t sd = (sd_t) lua_unboxinteger(L, 1, SD_TYPENAME); const int optname = luaL_checkoption(L, OPT_START, NULL, opt_names); const int level = (optname < OPTNAMES_TCP) ? SOL_SOCKET : (optname < OPTNAMES_IP ? IPPROTO_TCP : IPPROTO_IP); const int optflag = opt_flags[optname]; int optval[4]; socklen_t optlen = sizeof(int); const int nargs = lua_gettop(L); if (nargs > OPT_START) { optval[0] = lua_tointeger(L, OPT_START + 1); if (nargs > OPT_START + 1) { optval[1] = lua_tointeger(L, OPT_START + 2); optlen *= 2; } if (!setsockopt(sd, level, optflag, (char *) &optval, optlen)) { lua_settop(L, 1); return 1; } } else if (!getsockopt(sd, level, optflag, (char *) &optval, &optlen)) { lua_pushinteger(L, optval[0]); if (optlen <= sizeof(int)) return 1; lua_pushinteger(L, optval[1]); return 2; } return sys_seterror(L, 0); }
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; } }
/* Dispatch to the proper function based on the type string. */ static struct zip_source* S_create_source(lua_State* L, struct zip* ar) { static const char* types[] = { "file", "string", "zip", }; static S_src_t* fns[] = { &S_create_source_file, &S_create_source_string, &S_create_source_zip, }; if ( NULL == ar ) return NULL; return fns[luaL_checkoption(L, 3, NULL, types)](L, ar); }
static int side_get(lua_State *L) { side_t *side = *((side_t **)luaL_checkudata(L, 1, META_SIDE)); enum side_e field = luaL_checkoption(L, 2, side_opt[0], side_opt); if (!side) { if (field == side_valid) { lua_pushboolean(L, 0); return 1; } return luaL_error(L, "accessed side_t doesn't exist anymore."); } switch(field) { case side_valid: // valid lua_pushboolean(L, 1); return 1; case side_textureoffset: lua_pushinteger(L, side->textureoffset); return 1; case side_rowoffset: lua_pushinteger(L, side->rowoffset); return 1; case side_toptexture: lua_pushinteger(L, side->toptexture); return 1; case side_bottomtexture: lua_pushinteger(L, side->bottomtexture); return 1; case side_midtexture: lua_pushinteger(L, side->midtexture); return 1; case side_sector: LUA_PushUserdata(L, side->sector, META_SECTOR); return 1; case side_special: lua_pushinteger(L, side->special); return 1; case side_repeatcnt: lua_pushinteger(L, side->repeatcnt); return 1; case side_text: lua_pushstring(L, side->text); return 1; } return 0; }
static int colorlabel_newindex(lua_State *L) { int imgid; luaA_to(L,dt_lua_image_t,&imgid,-3); int colorlabel_index = luaL_checkoption(L,-2,NULL,dt_colorlabels_name); if(lua_toboolean(L,-1)) // no testing of type so we can benefit from all types of values { dt_colorlabels_set_label(imgid,colorlabel_index); } else { dt_colorlabels_remove_label(imgid,colorlabel_index); } return 0; }
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; }