+void lua_setupdebugsocket(void) {
+#ifdef LUA_DEBUGSOCKET
+
+ debugsocket = socket(AF_INET, SOCK_DGRAM, 0);
+ if(debugsocket < 0) {
+ debugsocket = -1;
+ Error("lua", ERR_ERROR, "Cannot create debug socket.");
+
+ return;
+ }
+
+ memset(&debugsocketdest, 0, sizeof(debugsocketdest));
+
+ debugsocketdest.sin_family = AF_INET;
+ debugsocketdest.sin_port = htons(LUA_DEBUGSOCKET_PORT);
+ debugsocketdest.sin_addr.s_addr = inet_addr(LUA_DEBUGSOCKET_ADDRESS);
+
+#endif
+}
+
+void lua_freedebugsocket(void) {
+#ifdef LUA_DEBUGSOCKET
+
+ if(debugsocket == -1)
+ return;
+
+ close(debugsocket);
+
+
+ debugsocket = -1;
+
+#endif
+}
+
+#ifdef LUA_DEBUGSOCKET
+void lua_debugoutput(char *format, ...) {
+ char buf[1024];
+ va_list va;
+ int size;
+
+ if(debugsocket == -1)
+ return;
+
+ va_start(va, format);
+ size = vsnprintf(buf, sizeof(buf), format, va);
+ va_end(va);
+
+ if(size >= sizeof(buf))
+ size = sizeof(buf) - 1;
+
+ if(size > 0)
+ sendto(debugsocket, buf, size, 0, (struct sockaddr *)&debugsocketdest, sizeof(debugsocketdest));
+}
+#endif
+
+lua_list *lua_listfromstate(lua_State *l) {
+ lua_list *i = lua_head;
+ for(;i;i=i->next)
+ if(i->l == l)
+ return i;
+
+ return &dummy;
+}
+
+int lua_listexists(lua_list *l) {
+ lua_list *i;
+
+ for(i=lua_head;i;i=i->next)
+ if(i == l)
+ return 1;
+
+ return 0;
+}
+
+int lua_lineok(const char *data) {
+ if(strchr(data, '\r') || strchr(data, '\n'))
+ return 0;
+ return 1;
+}
+
+int lua_debugpcall(lua_State *l, char *message, int a, int b, int c) {
+ lua_list *l2 = lua_listfromstate(l);
+ int ret;
+
+#ifdef LUA_DEBUGSOCKET
+ DEBUGOUT("%s: %s\n", l2->name->content, message);
+#endif
+
+#ifdef LUA_PROFILE
+ ACCOUNTING_START(l2);
+#endif
+
+ ret = lua_pcall(l, a, b, c);
+
+#ifdef LUA_PROFILE
+ ACCOUNTING_STOP(l2);
+#endif
+
+ return ret;
+}