Browse Source

Get rid of old C stuff.

Beoran 8 years ago
parent
commit
496557e0a4
31 changed files with 0 additions and 7818 deletions
  1. 0 25
      include/callrb.h
  2. 0 76
      include/client.h
  3. 0 19
      include/config.h
  4. 0 117
      include/dynar.h
  5. 0 78
      include/esh.h
  6. 0 93
      include/every.h
  7. 0 672
      include/libtelnet.h
  8. 0 58
      include/mem.h
  9. 0 88
      include/monolog.h
  10. 0 74
      include/rh.h
  11. 0 67
      include/server.h
  12. 0 22
      include/state.h
  13. 0 44
      include/timer.h
  14. 0 23
      include/toruby.h
  15. 0 30
      include/tr_file.h
  16. 0 151
      include/tr_macro.h
  17. 0 58
      oldsrc/callrb.c
  18. 0 227
      oldsrc/client.c
  19. 0 61
      oldsrc/config.c
  20. 0 728
      oldsrc/dynar.c
  21. 0 360
      oldsrc/esh.c
  22. 0 117
      oldsrc/every.c
  23. 0 1549
      oldsrc/libtelnet.c
  24. 0 122
      oldsrc/main.c
  25. 0 257
      oldsrc/mem.c
  26. 0 306
      oldsrc/monolog.c
  27. 0 481
      oldsrc/rh.c
  28. 0 850
      oldsrc/server.c
  29. 0 139
      oldsrc/timer.c
  30. 0 530
      oldsrc/toruby.c
  31. 0 396
      oldsrc/tr_file.c

+ 0 - 25
include/callrb.h

@@ -1,25 +0,0 @@
-#ifndef collide_H_INCLUDED
-#define collide_H_INCLUDED
-
-#include "state.h"
-
-enum CollisionKinds_ {
-  COLLIDE_BEGIN     = 1,
-  COLLIDE_COLLIDING = 2,
-  COLLIDE_END       = 3
-};
-
-int callrb_sprite_event(SpriteState * spritestate, int kind, void * data);
-
-int callrb_on_start();
-
-int callrb_on_reload();
-
-int callrb_on_update(State * self);
-
-
-#endif
-
-
-
-

+ 0 - 76
include/client.h

@@ -1,76 +0,0 @@
-#ifndef WOE_CLIENT_H
-#define WOE_CLIENT_H
-
-#if !defined(_POSIX_SOURCE)
-#define _POSIX_SOURCE
-#endif
-#if !defined(_BSD_SOURCE)
-#define _BSD_SOURCE
-#endif
-
-#define _XOPEN_SOURCE      
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <poll.h>
-#include <unistd.h>
-
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <ctype.h>
-
-#include "libtelnet.h"
-
-
-#ifndef WOE_CLIENTS_MAX
-/* Must beless than ulimit -nor */
-#define WOE_CLIENTS_MAX 1000
-#endif
-
-#ifndef WOE_CLIENT_BUFFER_SIZE 
-#define WOE_CLIENT_BUFFER_SIZE 256
-#endif
-
-struct woe_server;
-
-struct woe_client {  
-  struct woe_server * server;
-  int                 index;
-  int                 sock;
-  telnet_t *          telnet;
-  char                linebuf[256];
-  int                 linepos;
-  int                 busy;    
-  struct sockaddr_in  addr;
-  socklen_t           addrlen;
-};
-
-
-int woe_clients_max(void);
-
-int woe_client_iac(struct woe_client * cli, int cmd);
-int woe_client_input(struct woe_client * cli, const char * buffer, size_t size);
-int woe_client_zmp(struct woe_client * cli, int argc, const char *argv[]);
-int woe_client_negotiate(struct woe_client * cli, int how, int option);
-int woe_client_subnegotiate(struct woe_client * cli, const char * buf, int len, int telopt);
-int woe_client_ttype(struct woe_client * cli, int cmd, const char * name);
-int woe_client_error(struct woe_client * cli, int code, const char * msg);
-int woe_client_warning(struct woe_client * cli, int code, const char * msg);
-int woe_client_compress(struct woe_client * cli, int state);
-int woe_client_environ(struct woe_client * cli, int cmd, const struct telnet_environ_t *values, size_t size);
-int woe_client_mssp(struct woe_client * cli, const struct telnet_environ_t *values, size_t size);
- 
-
-struct woe_client * 
-woe_client_new(struct woe_server * srv,  int index, int socket, 
-               struct sockaddr_in *addr, socklen_t addrlen);
-
-struct woe_client * woe_client_free(struct woe_client * cli);
-
-
-
-#endif

+ 0 - 19
include/config.h

@@ -1,19 +0,0 @@
-
-#ifndef WOE_CONFIG_H
-#define WOE_CONFIG_H
-
-
-struct woe_config {
-  int     port;
-  char *  data_dir;
-  char *  log_file;
-};
-
-int woe_config_init_args(struct woe_config * config, int argc, char * argv[]);
-
-struct woe_config woe_config_get();
-struct woe_config woe_config_put(struct woe_config * config);
-
-
-  
-#endif  

+ 0 - 117
include/dynar.h

@@ -1,117 +0,0 @@
-#ifndef ERUTA_DYNAR_H
-#define ERUTA_DYNAR_H
-
-#include <stdlib.h> // need size_t
-// can use with every, so include the header 
-#include "every.h"
-#include "mem.h"
-
-#ifndef FALSE
-#define FALSE 0
-#endif
-
-#ifndef TRUE
-#define TRUE 1
-#endif
-
-typedef struct Dynar_ Dynar;
-
-typedef struct Lilis_ Lilis;
-
-typedef int DynarCompare(const void * one, const void * two);
-
-int dynar_size (Dynar * self );
-int dynar_amount (Dynar * self );
-int dynar_elementsize (Dynar * self );
-Dynar * dynar_done (Dynar * self );
-Dynar * dynar_destroy (Dynar * self , MemDestructor * destroy );
-Dynar * dynar_free (Dynar * self );
-Dynar * dynar_free_destroy (Dynar * self , MemDestructor * destroy );
-Dynar * dynar_alloc(void);
-Dynar * dynar_initempty (Dynar * self , int elsz );
-Dynar * dynar_newempty (int elsz );
-Dynar * dynar_size_ (Dynar * self , int newsize );
-Dynar * dynar_init (Dynar * self , int amount , int elsz );
-Dynar * dynar_initptr (Dynar * self , int amount );
-Dynar * dynar_new (int amount , int elsz );
-Dynar * dynar_newptr (int amount );
-Dynar * dynar_grow (Dynar * self , int amount );
-int dynar_sizeindex_ok (Dynar * self , int index );
-int dynar_index_ok (Dynar * self , int index );
-void * dynar_getraw_unsafe (Dynar * self , int index );
-void * dynar_getcopy_unsafe (Dynar * self , int index , void * out );
-Dynar * dynar_putraw_unsafe (Dynar * self , int index , void * ptr );
-void * dynar_getraw (Dynar * self , int index );
-void * dynar_getcopy (Dynar * self , int index , void * ptr );
-Dynar * dynar_putraw (Dynar * self , int index , void * ptr );
-void * dynar_putptr (Dynar * self , int index , void * ptr );
-void * dynar_getptr (Dynar * self , int index );
-void * dynar_putdata (Dynar * self , int index , void * ptr );
-void * dynar_getdata (Dynar * self , int index );
-
-Dynar * dynar_putnullall(Dynar * self);
-
-Dynar * dynar_qsort(Dynar * self  , DynarCompare * compare);
-void * dynar_lsearch(Dynar * self, const void * key, DynarCompare * compare);
-void * dynar_bsearch(Dynar * self, const void * key, DynarCompare * compare);
-
-
-Every * dynar_everynow_data (Every * every );
-Every * dynar_everynow_ptr (Every * every );
-Every * dynar_everyinit_data (Every * every );
-Every * dynar_everynext_data (Every * every );
-void * dynar_everyput_data (Every * every , void * data );
-void * dynar_everydone (Every * every );
-Every * dynar_everyinit_ptr (Every * every );
-Every * dynar_everynext_ptr (Every * every );
-void * dynar_everyput_ptr (Every * every , void * data );
-Every * dynar_every_data (Dynar * dynar );
-Every * dynar_every_ptr (Dynar * dynar );
-
-void * dynar_each_ptr (Dynar * self , EachDo * eachdo , void * extra );
-void * dynar_each_data(Dynar * self, EachDo * eachdo, void * extra);
-
-void * dynar_walkptr(Dynar * self, Walker * walker, void * extra);
-void * dynar_walkptrbetween(Dynar * self, int low, int high, 
-                            Walker * walker, void * extra);
-void * dynar_walkdata(Dynar * self, Walker * walker, void * extra);
-void * dynar_walkmapptrbetween(Dynar * self, int low, int high, 
-                            Walker * walker, void * extra);
-void * dynar_walkmapptr(Dynar * self, Walker * walker, void * extra);
-
-Dynar * dynar_resize(Dynar * self, int newsize, MemDestructor * destroy);
-
-
-Dynar * dynar_new_long();
-Dynar * dynar_put_long(Dynar * self, int index, long value);
-Dynar * dynar_get_long(Dynar * self, int index, long * value);
-Dynar * dynar_append_long(Dynar * self, long value);
-
-Dynar * dynar_destroy_structs(Dynar * self, MemDestructor * destroy);
-Dynar * dynar_destroy_structs_and_free(Dynar * self, MemDestructor * destroy);
-
-
-Lilis * lilis_freetail (Lilis * self );
-Lilis * lilis_done (Lilis * self );
-Lilis * lilis_free (Lilis * self );
-Lilis * lilis_alloc (void);
-Lilis * lilis_init (Lilis * self , Lilis * next , Lilis * prev , void * data );
-Lilis * lilis_initempty (Lilis * self );
-Lilis * lilis_new (Lilis * next , Lilis * prev , void * data );
-Lilis * lilis_newempty(void);
-Lilis * lilis_add (Lilis * self , Lilis * other );
-Lilis * lilis_addnew (Lilis * self , void * data );
-Lilis * lilis_removenext (Lilis * self );
-Lilis * lilis_remove (Lilis * self );
-Lilis * lilis_erase (Lilis * self );
-Lilis * lilis_erasenext (Lilis * self );
-Lilis * lilis_next (Lilis * self );
-Lilis * lilis_previous (Lilis * self );
-void * lilis_data (Lilis * self );
-
-
-
-
-#endif
-
-

+ 0 - 78
include/esh.h

@@ -1,78 +0,0 @@
-#ifndef ESH_H_INCLUDED
-#define EHS_H_INCLUDED
-
-/* Explicit String Handling. These functions help handling strings directly 
- * using struct wrappers, you need to pass in the parameters explicitly and 
- * most often by reference.
- * 
- * Nevertheless a struct wrapper is also provided , named woesb, 
- * or WOE String Buffer.
- */
-#include <stdlib.h> 
-#include <stdio.h>
-#include <stdarg.h> 
-
- 
-int    esh_bad_p(char **me, size_t * size, size_t * space);
-char * esh_make_empty(char ** me, size_t * size, size_t * space);
-char * esh_nul_terminate(char ** me, size_t * size, size_t * space);
-char * esh_alloc(char ** me, size_t * size, size_t * space, size_t init_space);
-char * esh_grow(char ** me, size_t  * size, size_t * space, size_t new_space);
-char * esh_ensure_space(char ** me, size_t * size, size_t * space, size_t grow_by);
-char * esh_append_char(char ** me, size_t * size, size_t * space, char ch);
-char * esh_append_buf(char  ** me, size_t * size, size_t * space, char * buf, size_t bufsize);
-char * esh_append_cstr(char ** me, size_t * size, size_t * space, char * str);
-char * esh_init_buf(char ** me, size_t * size, size_t * space, char * buf, size_t bufsize);
-char * esh_init_cstr(char ** me, size_t * size, size_t * space, char * buf);
-char * esh_new_buf(char **me, size_t * size, size_t * space, char * buf, size_t bufsize);
-char * esh_new(char **me, size_t * size, size_t * space, char * init);
-char * esh_new_empty(char **me, size_t * size, size_t * space);
-char * esh_free(char ** me, size_t * size, size_t * space);
-char * esh_read_file(char ** me, size_t * size, size_t * space, FILE * file);
-char * esh_read_filename(char ** me, size_t * size, size_t * space, char * fn);
-
-
-char * esh_join(char ** me, size_t * size, size_t * space,  ...);
-char * esh_join_va(char ** me, size_t * size, size_t * space, va_list strings);
-char * esh_init_join_cstr(char ** me, size_t * size, size_t * space, char * first, ...);
-char * esh_init_join_cstr_va(char ** me, size_t * size, size_t * space, char * first, va_list strings);
-
-
-
-struct woesb {
-  char      * text;
-  size_t      size;
-  size_t      space;  
-};
- 
-int woesb_bad_p(struct woesb * me);
-struct woesb * woesb_make_empty(struct woesb * me);
-struct woesb * woesb_nul_terminate(struct woesb * me);
-struct woesb * woesb_alloc(struct woesb * me, size_t init_space);
-struct woesb * woesb_grow(struct woesb * me, size_t new_space);
-struct woesb * woesb_ensure_space(struct woesb * me, size_t grow_by);
-struct woesb * woesb_append_char(struct woesb * me, char ch);
-struct woesb * woesb_append_buf(struct woesb * me, char * buf, size_t bufsize);
-struct woesb * woesb_append_cstr(struct woesb * me, char * str);
-struct woesb * woesb_init_buf(struct woesb * me, char * buf, size_t bufsize);
-struct woesb * woesb_init_cstr(struct woesb * me, char * buf);
-struct woesb * woesb_new_buf(struct woesb * me, char * buf, size_t bufsize);
-struct woesb * woesb_new(struct woesb * me, char * init);
-struct woesb * woesb_new_empty(struct woesb * me);
-struct woesb * woesb_free(struct woesb * me);
-struct woesb * woesb_read_file(struct woesb * me, FILE * file);
-struct woesb * woesb_read_filename(struct woesb * me, char * fn); 
-
-struct woesb * woesb_join(struct woesb * me, ...);
-struct woesb * woesb_join_va(struct woesb * me, va_list strings);
-struct woesb * woesb_init_join(struct woesb * me, char * first, ...);
-struct woesb * woesb_init_join_va(struct woesb * me, char * first, va_list strings);
-struct woesb * woesb_new_join_va(struct woesb * me, char * first, va_list strings);
-struct woesb * woesb_new_join(struct woesb * me, char * first, ...);
-
-
-#endif
-
-
-
-

+ 0 - 93
include/every.h

@@ -1,93 +0,0 @@
-#ifndef EVERY_H_INCLUDED
-#define EVERY_H_INCLUDED
-
-/*
-* Every is an iterator that allows to iterate over every element of
-* a collection.
-*/
-
-struct Every_;
-typedef struct Every_ Every;
-
-struct EveryActs_;
-typedef struct EveryActs_ EveryActs;
-
-
-// types for the funtion pointers that every uses
-typedef Every * (EveryNext(Every * every));
-typedef void  * (EveryPut(Every * every, void * element));
-typedef void  * (EveryDone(Every * every));
-typedef Every * (EveryInit(Every * every));
-
-// Struct with all function pointers for Every
-struct EveryActs_ {
-  EveryDone * done;
-  EveryInit * init;
-  EveryNext * next;
-  EveryPut  * put;
-};
-
-/*
-* Every is a struct that can be used with collections like Dynar to fetch
-* every element of the collection.
-*/
-struct Every_ {
-  void         * now;
-  void         * on;
-  int            index;
-  void         * extra;
-  EveryActs    * acts;
-};
-
-
-struct Each_;
-typedef struct Each_ Each;
-/*
-* Each is an alternative iterator interface, that
-* requires a function callback in stead of an object. 
-*/
-struct Each_ {
-  void * on;
-  void * now;
-  void * extra;
-  int    index;
-};
-
-/* Function pointer for the each iteration method.  */
-typedef void * (EachDo(Each * element));
-
-/* An even simpler iterator interface, one for simply iterating and one 
-for searching. Iteration is stopped if AllData returns not-NULL, 
-and the returned data will be returned by the function that uses AllData 
-too. Extra is used for passing in extra data if any. */
-typedef void * (AllData)(void * data, void * extra);
-
-
-/* Generic comparator function. Much like strcmp. */
-typedef int (AllCompare)(void * one, void * two);
-
-Every * every_alloc (void);
-Every * every_done (Every * self );
-Every * every_free (Every * self );
-Every * every_init (Every * self , EveryActs * acts );
-Every * every_new (EveryActs * acts );
-void * every_get (Every * self );
-void * every_put (Every * self , void * data );
-
-Every * every_next (Every * self , void * data );
-Each * each_init (Each * self , void * on , void * data );
-Each * each_next (Each * self , void * now );
-
-void * each_now (Each * self );
-void * each_on (Each * self );
-void * each_extra (Each * self );
-int each_index (Each * self);
-
-/* Yet another iteration interface: a Walker is a simple callback function. */
-typedef void * (Walker)(void * element, void * extra);  
-
-// and some helper macros
-#define EACH_NOW(EACH, TYPE) ((TYPE *) each_now(EACH))
-
-
-#endif

+ 0 - 672
include/libtelnet.h

@@ -1,672 +0,0 @@
-/*!
- * \brief libtelnet - TELNET protocol handling library
- *
- * SUMMARY:
- *
- * libtelnet is a library for handling the TELNET protocol.  It includes
- * routines for parsing incoming data from a remote peer as well as formatting
- * data to send to the remote peer.
- *
- * libtelnet uses a callback-oriented API, allowing application-specific
- * handling of various events.  The callback system is also used for buffering
- * outgoing protocol data, allowing the application to maintain control over
- * the actual socket connection.
- *
- * Features supported include the full TELNET protocol, Q-method option
- * negotiation, ZMP, MCCP2, MSSP, and NEW-ENVIRON.
- *
- * CONFORMS TO:
- *
- * RFC854  - http://www.faqs.org/rfcs/rfc854.html
- * RFC855  - http://www.faqs.org/rfcs/rfc855.html
- * RFC1091 - http://www.faqs.org/rfcs/rfc1091.html
- * RFC1143 - http://www.faqs.org/rfcs/rfc1143.html
- * RFC1408 - http://www.faqs.org/rfcs/rfc1408.html
- * RFC1572 - http://www.faqs.org/rfcs/rfc1572.html
- *
- * LICENSE:
- *
- * The author or authors of this code dedicate any and all copyright interest
- * in this code to the public domain. We make this dedication for the benefit
- * of the public at large and to the detriment of our heirs and successors. We
- * intend this dedication to be an overt act of relinquishment in perpetuity of
- * all present and future rights to this code under copyright law.
- *
- * \file libtelnet.h
- *
- * \version 0.21
- *
- * \author Sean Middleditch <sean@sourcemud.org>
- */
-
-#if !defined(LIBTELNET_INCLUDE)
-#define LIBTELNET_INCLUDE 1
-
-/* standard C headers necessary for the libtelnet API */
-#include <stdarg.h>
-
-/* C++ support */
-#if defined(__cplusplus)
-extern "C" {
-#endif
-
-/* printf type checking feature in GCC and some other compilers */
-#if __GNUC__
-# define TELNET_GNU_PRINTF(f,a) __attribute__((format(printf, f, a))) /*!< internal helper */
-# define TELNET_GNU_SENTINEL __attribute__((sentinel)) /*!< internal helper */
-#else
-# define TELNET_GNU_PRINTF(f,a) /*!< internal helper */
-# define TELNET_GNU_SENTINEL /*!< internal helper */
-#endif
-
-/*! Telnet state tracker object type. */
-typedef struct telnet_t telnet_t;
-
-/*! Telnet event object type. */
-typedef union telnet_event_t telnet_event_t;
-
-/*! Telnet option table element type. */
-typedef struct telnet_telopt_t telnet_telopt_t;
-
-/*! \name Telnet commands */
-/*@{*/
-/*! Telnet commands and special values. */
-#define TELNET_IAC 255
-#define TELNET_DONT 254
-#define TELNET_DO 253
-#define TELNET_WONT 252
-#define TELNET_WILL 251
-#define TELNET_SB 250
-#define TELNET_GA 249
-#define TELNET_EL 248
-#define TELNET_EC 247
-#define TELNET_AYT 246
-#define TELNET_AO 245
-#define TELNET_IP 244
-#define TELNET_BREAK 243
-#define TELNET_DM 242
-#define TELNET_NOP 241
-#define TELNET_SE 240
-#define TELNET_EOR 239
-#define TELNET_ABORT 238
-#define TELNET_SUSP 237
-#define TELNET_EOF 236
-/*@}*/
-
-/*! \name Telnet option values. */
-/*@{*/
-/*! Telnet options. */
-#define TELNET_TELOPT_BINARY 0
-#define TELNET_TELOPT_ECHO 1
-#define TELNET_TELOPT_RCP 2
-#define TELNET_TELOPT_SGA 3
-#define TELNET_TELOPT_NAMS 4
-#define TELNET_TELOPT_STATUS 5
-#define TELNET_TELOPT_TM 6
-#define TELNET_TELOPT_RCTE 7
-#define TELNET_TELOPT_NAOL 8
-#define TELNET_TELOPT_NAOP 9
-#define TELNET_TELOPT_NAOCRD 10
-#define TELNET_TELOPT_NAOHTS 11
-#define TELNET_TELOPT_NAOHTD 12
-#define TELNET_TELOPT_NAOFFD 13
-#define TELNET_TELOPT_NAOVTS 14
-#define TELNET_TELOPT_NAOVTD 15
-#define TELNET_TELOPT_NAOLFD 16
-#define TELNET_TELOPT_XASCII 17
-#define TELNET_TELOPT_LOGOUT 18
-#define TELNET_TELOPT_BM 19
-#define TELNET_TELOPT_DET 20
-#define TELNET_TELOPT_SUPDUP 21
-#define TELNET_TELOPT_SUPDUPOUTPUT 22
-#define TELNET_TELOPT_SNDLOC 23
-#define TELNET_TELOPT_TTYPE 24
-#define TELNET_TELOPT_EOR 25
-#define TELNET_TELOPT_TUID 26
-#define TELNET_TELOPT_OUTMRK 27
-#define TELNET_TELOPT_TTYLOC 28
-#define TELNET_TELOPT_3270REGIME 29
-#define TELNET_TELOPT_X3PAD 30
-#define TELNET_TELOPT_NAWS 31
-#define TELNET_TELOPT_TSPEED 32
-#define TELNET_TELOPT_LFLOW 33
-#define TELNET_TELOPT_LINEMODE 34
-#define TELNET_TELOPT_XDISPLOC 35
-#define TELNET_TELOPT_ENVIRON 36
-#define TELNET_TELOPT_AUTHENTICATION 37
-#define TELNET_TELOPT_ENCRYPT 38
-#define TELNET_TELOPT_NEW_ENVIRON 39
-#define TELNET_TELOPT_MSSP 70
-#define TELNET_TELOPT_COMPRESS 85
-#define TELNET_TELOPT_COMPRESS2 86
-#define TELNET_TELOPT_ZMP 93
-#define TELNET_TELOPT_EXOPL 255
-
-#define TELNET_TELOPT_MCCP2 86
-/*@}*/
-
-/*! \name Protocol codes for TERMINAL-TYPE commands. */
-/*@{*/
-/*! TERMINAL-TYPE codes. */
-#define TELNET_TTYPE_IS 0
-#define TELNET_TTYPE_SEND 1
-/*@}*/
-
-/*! \name Protocol codes for NEW-ENVIRON/ENVIRON commands. */
-/*@{*/
-/*! NEW-ENVIRON/ENVIRON codes. */
-#define TELNET_ENVIRON_IS 0
-#define TELNET_ENVIRON_SEND 1
-#define TELNET_ENVIRON_INFO 2
-#define TELNET_ENVIRON_VAR 0
-#define TELNET_ENVIRON_VALUE 1
-#define TELNET_ENVIRON_ESC 2
-#define TELNET_ENVIRON_USERVAR 3
-/*@}*/
-
-/*! \name Protocol codes for MSSP commands. */
-/*@{*/
-/*! MSSP codes. */
-#define TELNET_MSSP_VAR 1
-#define TELNET_MSSP_VAL 2
-/*@}*/
-
-/*! \name Telnet state tracker flags. */
-/*@{*/
-/*! Control behavior of telnet state tracker. */
-#define TELNET_FLAG_PROXY (1<<0)
-
-#define TELNET_PFLAG_DEFLATE (1<<7)
-/*@}*/
-
-/*! 
- * error codes 
- */
-enum telnet_error_t {
-	TELNET_EOK = 0,   /*!< no error */
-	TELNET_EBADVAL,   /*!< invalid parameter, or API misuse */
-	TELNET_ENOMEM,    /*!< memory allocation failure */
-	TELNET_EOVERFLOW, /*!< data exceeds buffer size */
-	TELNET_EPROTOCOL, /*!< invalid sequence of special bytes */
-	TELNET_ECOMPRESS  /*!< error handling compressed streams */
-};
-typedef enum telnet_error_t telnet_error_t; /*!< Error code type. */
-
-/*! 
- * event codes 
- */
-enum telnet_event_type_t {
-	TELNET_EV_DATA = 0,        /*!< raw text data has been received */
-	TELNET_EV_SEND,            /*!< data needs to be sent to the peer */
-	TELNET_EV_IAC,             /*!< generic IAC code received */
-	TELNET_EV_WILL,            /*!< WILL option negotiation received */
-	TELNET_EV_WONT,            /*!< WONT option neogitation received */
-	TELNET_EV_DO,              /*!< DO option negotiation received */
-	TELNET_EV_DONT,            /*!< DONT option negotiation received */
-	TELNET_EV_SUBNEGOTIATION,  /*!< sub-negotiation data received */
-	TELNET_EV_COMPRESS,        /*!< compression has been enabled */
-	TELNET_EV_ZMP,             /*!< ZMP command has been received */
-	TELNET_EV_TTYPE,           /*!< TTYPE command has been received */
-	TELNET_EV_ENVIRON,         /*!< ENVIRON command has been received */
-	TELNET_EV_MSSP,            /*!< MSSP command has been received */
-	TELNET_EV_WARNING,         /*!< recoverable error has occured */
-	TELNET_EV_ERROR            /*!< non-recoverable error has occured */
-};
-typedef enum telnet_event_type_t telnet_event_type_t; /*!< Telnet event type. */
-
-/*! 
- * environ/MSSP command information 
- */
-struct telnet_environ_t {
-	unsigned char type; /*!< either TELNET_ENVIRON_VAR or TELNET_ENVIRON_USERVAR */
-	char *var;          /*!< name of the variable being set */
-	char *value;        /*!< value of variable being set; empty string if no value */
-};
-
-/*! 
- * event information 
- */
-union telnet_event_t {
-	/*! 
-	 * \brief Event type
-	 *
-	 * The type field will determine which of the other event structure fields
-	 * have been filled in.  For instance, if the event type is TELNET_EV_ZMP,
-	 * then the zmp event field (and ONLY the zmp event field) will be filled
-	 * in.
-	 */ 
-	enum telnet_event_type_t type;
-
-	/*! 
-	 * data event: for DATA and SEND events 
-	 */
-	struct data_t {
-		enum telnet_event_type_t _type; /*!< alias for type */
-		const char *buffer;             /*!< byte buffer */
-		size_t size;                    /*!< number of bytes in buffer */
-	} data;
-
-	/*! 
-	 * WARNING and ERROR events 
-	 */
-	struct error_t {
-		enum telnet_event_type_t _type; /*!< alias for type */
-		const char *file;               /*!< file the error occured in */
-		const char *func;               /*!< function the error occured in */
-		const char *msg;                /*!< error message string */
-		int line;                       /*!< line of file error occured on */
-		telnet_error_t errcode;         /*!< error code */
-	} error;
-
-	/*! 
-	 * command event: for IAC 
-	 */
-	struct iac_t {
-		enum telnet_event_type_t _type; /*!< alias for type */
-		unsigned char cmd;              /*!< telnet command received */
-	} iac;
-
-	/*! 
-	 * negotiation event: WILL, WONT, DO, DONT 
-	 */
-	struct negotiate_t {
-		enum telnet_event_type_t _type; /*!< alias for type */
-		unsigned char telopt;           /*!< option being negotiated */
-	} neg;
-
-	/*! 
-	 * subnegotiation event 
-	 */
-	struct subnegotiate_t {
-		enum telnet_event_type_t _type; /*!< alias for type */
-		const char *buffer;             /*!< data of sub-negotiation */
-		size_t size;                    /*!< number of bytes in buffer */
-		unsigned char telopt;           /*!< option code for negotiation */
-	} sub;
-
-	/*! 
-	 * ZMP event 
-	 */
-	struct zmp_t {
-		enum telnet_event_type_t _type; /*!< alias for type */
-		const char **argv;              /*!< array of argument string */
-		size_t argc;                    /*!< number of elements in argv */
-	} zmp;
-
-	/*! 
-	 * TTYPE event 
-	 */
-	struct ttype_t {
-		enum telnet_event_type_t _type; /*!< alias for type */
-		unsigned char cmd;              /*!< TELNET_TTYPE_IS or TELNET_TTYPE_SEND */
-		const char* name;               /*!< terminal type name (IS only) */
-	} ttype;
-
-	/*! 
-	 * COMPRESS event 
-	 */
-	struct compress_t {
-		enum telnet_event_type_t _type; /*!< alias for type */
-		unsigned char state;            /*!< 1 if compression is enabled,
-	                                         0 if disabled */
-	} compress;
-
-	/*! 
-	 * ENVIRON/NEW-ENVIRON event
-	 */
-	struct environ_t {
-		enum telnet_event_type_t _type;        /*!< alias for type */
-		const struct telnet_environ_t *values; /*!< array of variable values */
-		size_t size;                           /*!< number of elements in values */
-		unsigned char cmd;                     /*!< SEND, IS, or INFO */
-	} environ;
-	
-	/*!
-	 * MSSP event
-	 */
-	struct mssp_t {
-		enum telnet_event_type_t _type;        /*!< alias for type */
-		const struct telnet_environ_t *values; /*!< array of variable values */
-		size_t size;                           /*!< number of elements in values */
-	} mssp;
-};
-
-/*! 
- * \brief event handler
- *
- * This is the type of function that must be passed to
- * telnet_init() when creating a new telnet object.  The
- * function will be invoked once for every event generated
- * by the libtelnet protocol parser.
- *
- * \param telnet    The telnet object that generated the event
- * \param event     Event structure with details about the event
- * \param user_data User-supplied pointer
- */
-typedef void (*telnet_event_handler_t)(telnet_t *telnet,
-		telnet_event_t *event, void *user_data);
-
-/*! 
- * telopt support table element; use telopt of -1 for end marker 
- */
-struct telnet_telopt_t {
-	short telopt;      /*!< one of the TELOPT codes or -1 */
-	unsigned char us;  /*!< TELNET_WILL or TELNET_WONT */
-	unsigned char him; /*!< TELNET_DO or TELNET_DONT */
-};
-
-/*! 
- * state tracker -- private data structure 
- */
-struct telnet_t;
-
-/*!
- * \brief Initialize a telnet state tracker.
- *
- * This function initializes a new state tracker, which is used for all
- * other libtelnet functions.  Each connection must have its own
- * telnet state tracker object.
- *
- * \param telopts   Table of TELNET options the application supports.
- * \param eh        Event handler function called for every event.
- * \param flags     0 or TELNET_FLAG_PROXY.
- * \param user_data Optional data pointer that will be passsed to eh.
- * \return Telent state tracker object.
- */
-extern telnet_t* telnet_init(const telnet_telopt_t *telopts,
-		telnet_event_handler_t eh, unsigned char flags, void *user_data);
-
-/*!
- * \brief Free up any memory allocated by a state tracker.
- *
- * This function must be called when a telnet state tracker is no
- * longer needed (such as after the connection has been closed) to
- * release any memory resources used by the state tracker.
- *
- * \param telnet Telnet state tracker object.
- */
-extern void telnet_free(telnet_t *telnet);
-
-/*!
- * \brief Push a byte buffer into the state tracker.
- *
- * Passes one or more bytes to the telnet state tracker for
- * protocol parsing.  The byte buffer is most often going to be
- * the buffer that recv() was called for while handling the
- * connection.
- *
- * \param telnet Telnet state tracker object.
- * \param buffer Pointer to byte buffer.
- * \param size   Number of bytes pointed to by buffer.
- */
-extern void telnet_recv(telnet_t *telnet, const char *buffer,
-		size_t size);
-
-/*!
- * \brief Send a telnet command.
- *
- * \param telnet Telnet state tracker object.
- * \param cmd    Command to send.
- */
-extern void telnet_iac(telnet_t *telnet, unsigned char cmd);
-
-/*!
- * \brief Send negotiation command.
- *
- * Internally, libtelnet uses RFC1143 option negotiation rules.
- * The negotiation commands sent with this function may be ignored
- * if they are determined to be redundant.
- *
- * \param telnet Telnet state tracker object.
- * \param cmd    TELNET_WILL, TELNET_WONT, TELNET_DO, or TELNET_DONT.
- * \param opt    One of the TELNET_TELOPT_* values.
- */
-extern void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
-		unsigned char opt);
-
-/*!
- * Send non-command data (escapes IAC bytes).
- *
- * \param telnet Telnet state tracker object.
- * \param buffer Buffer of bytes to send.
- * \param size   Number of bytes to send.
- */
-extern void telnet_send(telnet_t *telnet,
-		const char *buffer, size_t size);
-
-/*!
- * \brief Begin a sub-negotiation command.
- *
- * Sends IAC SB followed by the telopt code.  All following data sent
- * will be part of the sub-negotiation, until telnet_finish_sb() is
- * called.
- *
- * \param telnet Telnet state tracker object.
- * \param telopt One of the TELNET_TELOPT_* values.
- */
-extern void telnet_begin_sb(telnet_t *telnet,
-		unsigned char telopt);
-
-/*!
- * \brief Finish a sub-negotiation command.
- *
- * This must be called after a call to telnet_begin_sb() to finish a
- * sub-negotiation command.
- *
- * \param telnet Telnet state tracker object.
- */
-#define telnet_finish_sb(telnet) telnet_iac((telnet), TELNET_SE)
-
-/*!
- * \brief Shortcut for sending a complete subnegotiation buffer.
- *
- * Equivalent to:
- *   telnet_begin_sb(telnet, telopt);
- *   telnet_send(telnet, buffer, size);
- *   telnet_finish_sb(telnet);
- *
- * \param telnet Telnet state tracker format.
- * \param telopt One of the TELNET_TELOPT_* values.
- * \param buffer Byte buffer for sub-negotiation data.
- * \param size   Number of bytes to use for sub-negotiation data.
- */
-extern void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
-		const char *buffer, size_t size);
-
-/*!
- * \brief Begin sending compressed data.
- *
- * This function will begein sending data using the COMPRESS2 option,
- * which enables the use of zlib to compress data sent to the client.
- * The client must offer support for COMPRESS2 with option negotiation,
- * and zlib support must be compiled into libtelnet.
- *
- * Only the server may call this command.
- *
- * \param telnet Telnet state tracker object.
- */
-extern void telnet_begin_compress2(telnet_t *telnet);
-
-/*!
- * \brief Send formatted data.
- *
- * This function is a wrapper around telnet_send().  It allows using
- * printf-style formatting.
- *
- * Additionally, this function will translate \\r to the CR NUL construct and
- * \\n with CR LF, as well as automatically escaping IAC bytes like
- * telnet_send().
- *
- * \param telnet Telnet state tracker object.
- * \param fmt    Format string.
- * \return Number of bytes sent.
- */
-extern int telnet_printf(telnet_t *telnet, const char *fmt, ...)
-		TELNET_GNU_PRINTF(2, 3);
-
-/*!
- * \brief Send formatted data.
- *
- * See telnet_printf().
- */
-extern int telnet_vprintf(telnet_t *telnet, const char *fmt, va_list va);
-
-/*!
- * \brief Send formatted data (no newline escaping).
- *
- * This behaves identically to telnet_printf(), except that the \\r and \\n
- * characters are not translated.  The IAC byte is still escaped as normal
- * with telnet_send().
- *
- * \param telnet Telnet state tracker object.
- * \param fmt    Format string.
- * \return Number of bytes sent.
- */
-extern int telnet_raw_printf(telnet_t *telnet, const char *fmt, ...)
-		TELNET_GNU_PRINTF(2, 3);
-
-/*!
- * \brief Send formatted data (no newline escaping).
- *
- * See telnet_raw_printf().
- */
-extern int telnet_raw_vprintf(telnet_t *telnet, const char *fmt, va_list va);
-
-/*!
- * \brief Begin a new set of NEW-ENVIRON values to request or send.
- *
- * This function will begin the sub-negotiation block for sending or
- * requesting NEW-ENVIRON values.
- *
- * The telnet_finish_newenviron() macro must be called after this
- * function to terminate the NEW-ENVIRON command.
- *
- * \param telnet Telnet state tracker object.
- * \param type   One of TELNET_ENVIRON_SEND, TELNET_ENVIRON_IS, or
- *               TELNET_ENVIRON_INFO.
- */
-extern void telnet_begin_newenviron(telnet_t *telnet, unsigned char type);
-
-/*!
- * \brief Send a NEW-ENVIRON variable name or value.
- *
- * This can only be called between calls to telnet_begin_newenviron() and
- * telnet_finish_newenviron().
- *
- * \param telnet Telnet state tracker object.
- * \param type   One of TELNET_ENVIRON_VAR, TELNET_ENVIRON_USERVAR, or
- *               TELNET_ENVIRON_VALUE.
- * \param string Variable name or value.
- */
-extern void telnet_newenviron_value(telnet_t* telnet, unsigned char type,
-		const char *string);
-
-/*!
- * \brief Finish a NEW-ENVIRON command.
- *
- * This must be called after a call to telnet_begin_newenviron() to finish a
- * NEW-ENVIRON variable list.
- *
- * \param telnet Telnet state tracker object.
- */
-#define telnet_finish_newenviron(telnet) telnet_finish_sb((telnet))
-
-/*!
- * \brief Send the TERMINAL-TYPE SEND command.
- *
- * Sends the sequence IAC TERMINAL-TYPE SEND.
- *
- * \param telnet Telnet state tracker object.
- */
-extern void telnet_ttype_send(telnet_t *telnet);
-
-/*!
- * \brief Send the TERMINAL-TYPE IS command.
- *
- * Sends the sequence IAC TERMINAL-TYPE IS "string".
- *
- * According to the RFC, the recipient of a TERMINAL-TYPE SEND shall
- * send the next possible terminal-type the client supports.  Upon sending
- * the type, the client should switch modes to begin acting as the terminal
- * type is just sent.
- *
- * The server may continue sending TERMINAL-TYPE IS until it receives a
- * terminal type is understands.  To indicate to the server that it has
- * reached the end of the available optoins, the client must send the last
- * terminal type a second time.  When the server receives the same terminal
- * type twice in a row, it knows it has seen all available terminal types.
- *
- * After the last terminal type is sent, if the client receives another
- * TERMINAL-TYPE SEND command, it must begin enumerating the available
- * terminal types from the very beginning.  This allows the server to
- * scan the available types for a preferred terminal type and, if none
- * is found, to then ask the client to switch to an acceptable
- * alternative.
- *
- * Note that if the client only supports a single terminal type, then
- * simply sending that one type in response to every SEND will satisfy
- * the behavior requirements.
- *
- * \param telnet Telnet state tracker object.
- * \param ttype  Name of the terminal-type being sent.
- */
-extern void telnet_ttype_is(telnet_t *telnet, const char* ttype);
-
-/*!
- * \brief Send a ZMP command.
- *
- * \param telnet Telnet state tracker object.
- * \param argc   Number of ZMP commands being sent.
- * \param argv   Array of argument strings.
- */
-extern void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv);
-
-/*!
- * \brief Send a ZMP command.
- *
- * Arguments are listed out in var-args style.  After the last argument, a
- * NULL pointer must be passed in as a sentinel value.
- *
- * \param telnet Telnet state tracker object.
- */
-extern void telnet_send_zmpv(telnet_t *telnet, ...) TELNET_GNU_SENTINEL;
-
-/*!
- * \brief Send a ZMP command.
- *
- * See telnet_send_zmpv().
- */
-extern void telnet_send_vzmpv(telnet_t *telnet, va_list va);
-
-/*!
- * \brief Begin sending a ZMP command
- *
- * \param telnet Telnet state tracker object.
- * \param cmd    The first argument (command name) for the ZMP command.
- */
-extern void telnet_begin_zmp(telnet_t *telnet, const char *cmd);
-
-/*!
- * \brief Send a ZMP command argument.
- *
- * \param telnet Telnet state tracker object.
- * \param arg    Telnet argument string.
- */
-extern void telnet_zmp_arg(telnet_t *telnet, const char *arg);
-
-/*!
- * \brief Finish a ZMP command.
- *
- * This must be called after a call to telnet_begin_zmp() to finish a
- * ZMP argument list.
- *
- * \param telnet Telnet state tracker object.
- */
-#define telnet_finish_zmp(telnet) telnet_finish_sb((telnet))
-
-/* C++ support */
-#if defined(__cplusplus)
-} /* extern "C" */
-#endif
-
-#endif /* !defined(LIBTELNET_INCLUDE) */

+ 0 - 58
include/mem.h

@@ -1,58 +0,0 @@
-#ifndef ERUTA_MEM_H
-#define ERUTA_MEM_H
-
-#include <stdlib.h>
-
-/* Wrapper for calloc/malloc */
-void * mem_calloc(size_t amount, size_t size);
-
-/* Wrapper for calloc/malloc */
-void * mem_alloc(size_t size);
-
-/* Wrapper for free */
-void * mem_free(void * ptr);
-
-/* Wrapper for realloc */
-void * mem_realloc(void *ptr, size_t newsize);
-
-/* Resizes memory, taking care not to lose ptr* if the reallocation
-fails. */
-void * mem_resize(void ** ptr, size_t newsize);
-
-/* Wrapper for memmove, for consistency */
-void * mem_move(void * dest, void * src, size_t size);
-
-/* A function pointer that can act as a destructor. 
-Should return NULL on sucessful freeing, and non-null 
-if freeing failed. */
-typedef void * (MemDestructor)(void * ptr);
-
-/* Counted reference. Just an idea... */
-struct Refc {
-  void          * ptr; 
-  int             refc;
-  MemDestructor * destroy;
-};
-
-
-
-/* Handy macros. */
-/*
-#define STRUCT_NALLOC(STRUCT, AMOUNT) \
-        ((STRUCT *) mem_calloc(sizeof(STRUCT), (AMOUNT)))
-*/
-
-#define MEM_ALLOCATE(STRUCTNAME) mem_alloc(sizeof(STRUCTNAME))
-
-#define STRUCT_NALLOC(STRUCT, AMOUNT) \
-        (mem_calloc(sizeof(STRUCT), (AMOUNT)))
-
-       
-#define STRUCT_ALLOC(STRUCT)          \
-        ((STRUCT *)MEM_ALLOCATE(STRUCT))
-        
-#define STRUCT_FREE(VAR)              \
-        mem_free(VAR)
-
-
-#endif

+ 0 - 88
include/monolog.h

@@ -1,88 +0,0 @@
-#ifndef monolog_H_INCLUDED
-#define monolog_H_INCLUDED
-
-#include <stdarg.h>
-#include <stdio.h>
-
-#ifndef FALSE
-#define FALSE 0
-#endif
-
-#ifndef TRUE
-#define TRUE 1
-#endif
-
-
-/* Finer grained logging. */
-
-/** The interface for a logger */
-struct MonologLoggerInterface {
-  /* Logger function. */ 
-  int   (*log)(char * file, int line, char * level, void * data, char * format, va_list args);
-  /* Optional destructor. */
-  void (*free)(void * data);
-}; 
-
-int monolog_add_logger(void * data, struct MonologLoggerInterface * logger);
-int monolog_remove_logger(int index);
-int monolog_enable_level(char * name);
-int monolog_disable_level(char * name);
-int monolog_log_va(char * file, int line, char * level, char * format, va_list args);
-int monolog_log(char * file, int line, char * level, char * format, ...);
-int monolog_init(void);
-void monolog_done();
-
-int monolog_stdout_logger(char * file, int line, char * level, void * data, char * format, va_list args);
-
-int monolog_stderr_logger(char * file, int line, char * level,  void * data, char * format, va_list args);
-
-/** Log function for logger that logs to a FILE. */
-int monolog_file_logger(char * file, int line, char * level,  void * data, char * format, va_list args);
-
-/** Free function for logger that logs to a FILE. */
-void monolog_file_free(void * data);
-
-
-/* Logger helper macros */
-#define DEFINE_LOGGER(NAME, LOGF, FREEF) \
-  static struct MonologLoggerInterface NAME  = { LOGF, FREEF }
-
-#define DEFINE_STDERR_LOGGER(NAME) DEFINE_LOGGER(NAME, monolog_stderr_logger, NULL)
-#define DEFINE_STDOUT_LOGGER(NAME) DEFINE_LOGGER(NAME, monolog_stdout_logger, NULL)
-#define DEFINE_FILE_LOGGER(NAME)   DEFINE_LOGGER(NAME, monolog_file_logger, monolog_file_free)
-
-/* Logging helper macros */
-
-#define LOG_LEVEL(LEVEL, ...) \
-  monolog_log(__FILE__, __LINE__, LEVEL, __VA_ARGS__)
-
-#define LOG(...)          LOG_LEVEL(__FILE__,   __VA_ARGS__)
-#define LOG_ERROR(...)    LOG_LEVEL("ERROR",    __VA_ARGS__)
-#define LOG_WARNING(...)  LOG_LEVEL("WARNING",  __VA_ARGS__)
-#define LOG_NOTE(...)     LOG_LEVEL("NOTE",     __VA_ARGS__)
-#define LOG_DEBUG(...)    LOG_LEVEL("DEBUG",    __VA_ARGS__)
-
-#define LOG_ENABLE(FORMAT)            \
-    monolog_enable_level(#FORMAT)
-
-#define LOG_ENABLE_ERROR()        LOG_ENABLE(ERROR)  
-#define LOG_ENABLE_WARNING()      LOG_ENABLE(WARNING)
-#define LOG_ENABLE_NOTE()         LOG_ENABLE(NOTE)
-#define LOG_ENABLE_DEBUG()        LOG_ENABLE(DEBUG)
-
-#define LOG_DISABLE(FORMAT)            \
-    monolog_disable_level(#FORMAT)
-    
-#define LOG_DISABLE_ERROR()       LOG_DISABLE(error)  
-#define LOG_DISABLE_WARNING()     LOG_DISABLE(warning)
-#define LOG_DISABLE_NOTE()        LOG_DISABLE(note)
-#define LOG_DISABLE_DEBUG()       LOG_DISABLE(debug)
-
-
-
-
-#endif
-
-
-
-

+ 0 - 74
include/rh.h

@@ -1,74 +0,0 @@
-#ifndef rh_H_INCLUDED
-#define rh_H_INCLUDED
-
-#include <stdarg.h>
-
-#include <mruby.h>
-#include <mruby/data.h>
-#include <mruby/compile.h>
-#include <mruby/proc.h>
-#include <mruby/string.h>
-
-// shortcut typedef. Also to alllow possible future enhancement.
-typedef mrb_state Ruby;
-
-typedef struct Script_ Script;
-
-
-Ruby * rh_new(void);
-Ruby * rh_free(Ruby * self);
-
-mrb_value rh_inspect(mrb_state *mrb , mrb_value obj );
-
-char * rh_inspect_cstr(mrb_state *mrb , mrb_value value);
-
-int rh_run_file(Ruby * self , const char * filename, FILE * file );
-
-int rh_run_filename (Ruby * self , const char * filename );
-
-/* rh_run_script only works fo files in a (sub) folder of the WOE
- * directory, where rh_run_filename is generic. */
-int rh_run_script(Ruby * self, const char * filename);
-
-char * rh_exception_string (Ruby * self );
-
-mrb_value rh_simple_funcall(Ruby * ruby, char * name);
-
-mrb_value rh_run_function_args(Ruby * ruby, mrb_value rubyself, 
-                          char * name, int argc, mrb_value * argv);
-
-mrb_value rh_run_toplevel_args(Ruby * ruby, char * name, int argc, mrb_value * argv);
-
-mrb_value rh_run_function_va(Ruby * ruby, mrb_value rubyself, 
-                          char * name, char * format, va_list args);
-
-mrb_value rh_run_toplevel_va(Ruby * ruby, char * name, char * format, va_list args);
-
-
-mrb_value rh_run_function(Ruby * ruby, mrb_value rubyself, char * name, char * format, ...);
-
-mrb_value rh_run_toplevel(Ruby * ruby, char * name, char * format, ...);
-int rh_tobool(mrb_value v);                              
-                              
-#define rh_bool_value(B) ( (B) ? mrb_true_value() : mrb_false_value())
-
-/* Tries to (re-)load the main ruby file, output to console. */
-int rh_load_main();
-
-Ruby * rh_open_ruby_state();
-Ruby * rh_close_ruby_state();
-Ruby * rh_get_ruby_state(Ruby * ruby);
-
-
-
-int rh_load_main();
-int rh_on_start();
-int rh_on_reload(); 
-
-
-
-#endif
-
-
-
-

+ 0 - 67
include/server.h

@@ -1,67 +0,0 @@
-#ifndef WOE_SERVER_H
-#define WOE_SERVER_H
-
-#ifndef WOE_TIMERS_MAX
-#define WOE_TIMERS_MAX 32
-#endif
-
-#include <stdarg.h>
-#include <mruby.h>
-
-struct woe_server;
-
-struct woe_server * woe_server_new(int port);
-struct woe_server * woe_server_free(struct woe_server * srv);
-
-int woe_server_listen(struct woe_server * srv);
-int woe_server_update(struct woe_server * srv, int timeout);
-
-int woe_server_busy(struct woe_server * srv);
-void woe_server_request_shutdown(struct woe_server * srv);
-
-enum woe_timer_type {
-  WOE_TIMER_TYPE_REPEAT,
-  WOE_TIMER_TYPE_ONCE
-};
-
-
-
-int woe_server_send_to_client(struct woe_server * srv, int client, char * data, size_t size);                           
-
-struct woe_server * woe_server_set_mrb(struct woe_server * srv, mrb_state * mrb);
-mrb_state * woe_server_get_mrb(struct woe_server * srv);
-
-int woe_server_disconnect_id(struct woe_server * srv, int id);
-
-
-int woe_server_iac(struct woe_server * srv, int client, int command);
-int woe_server_negotiate(struct woe_server * srv, int client, int how, int option);
-int woe_server_begin_sb(struct woe_server * srv, int client, int telopt);
-int woe_server_finish_sb(struct woe_server * srv, int client);
-int woe_server_subnegotiation(struct woe_server * srv, int client, int telopt, char * buffer, int size);
-int woe_server_begin_compress2(struct woe_server * srv, int client);
-int woe_server_vprintf(struct woe_server * srv, int client, const char *fmt, va_list va);
-int woe_server_printf(struct woe_server * srv, int client, const char *fmt, ...);
-int woe_server_raw_vprintf(struct woe_server * srv, int client, const char *fmt, va_list va);
-int woe_server_raw_printf(struct woe_server * srv, int client, const char *fmt, ...);
-int woe_server_begin_newenviron(struct woe_server * srv, int client, int type);
-int woe_server_newenviron_value(struct woe_server * srv, int client, int type, char * value);
-int woe_server_finish_newenviron(struct woe_server * srv, int client);
-int woe_server_ttype_send(struct woe_server * srv, int client);
-int woe_server_ttype_is(struct woe_server * srv, int client, char * ttype);
-int woe_server_send_zmp(struct woe_server * srv, int client, int argc, const char ** argv);
-int woe_server_send_vzmpv(struct woe_server * srv, int client, va_list va);
-int woe_server_send_zmpv(struct woe_server * srv, int client, ...);
-int woe_server_begin_zmp(struct woe_server * srv, int client, const char * cmd);
-int woe_server_zmp_arg(struct woe_server * srv, int client, const char * arg);
-int woe_server_finish_zmp(struct woe_server * srv, int client, const char * cmd);
-
-int woe_server_make_new_timer_id(struct woe_server * srv);
-struct woe_client * woe_server_remove_client(struct woe_server * srv, int index);
-struct woe_timer * woe_server_remove_timer(struct woe_server * srv, int index);
-int woe_server_set_timer_value(struct woe_server * srv, int index, double value, double interval);
-int woe_server_get_timer_value(struct woe_server * srv, int index, double * value, double * interval);
-                           
- 
-
-#endif

+ 0 - 22
include/state.h

@@ -1,22 +0,0 @@
-#ifndef STATE_H_INCLUDED
-#define STATE_H_INCLUDED
-
-#include "rh.h"
-#include "server.h"
-#include "config.h"
-
-/* All state of WOE in a handy struct */
-struct woe_state {
-  struct woe_server * server;
-  struct woe_config * config;
-  mrb_state         * mrb;
-};
-
-
-#define MRB_WOE_STATE(MRB) ((struct woe_state *)(MRB->ud))
-#define MRB_WOE_SERVER(MRB) (MRB_WOE_STATE(MRB)->server)
-#define MRB_WOE_CONFIG(MRB) (MRB_WOE_STATE(MRB)->config)
-
-
-#endif
-

+ 0 - 44
include/timer.h

@@ -1,44 +0,0 @@
-#ifndef WOE_TIMER_H
-#define WOE_TIMER_H
-
-#if !defined(_POSIX_C_SOURCE)
-#define _POSIX_C_SOURCE 200801L
-#endif
-
-#if !defined(_POSIX_SOURCE)
-#define _POSIX_SOURCE
-#endif
-#if !defined(_BSD_SOURCE)
-#define _BSD_SOURCE
-#endif
-
-#include <signal.h>
-#include <time.h>
-
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <ctype.h>
-
-
-struct woe_server;
-
-struct woe_timer {  
-  struct woe_server * server;
-  int                 index;
-  timer_t             timer;
-  int                 set;
-  struct sigaction    sa;
-};
-
-
-
-struct woe_timer * woe_timer_new(struct woe_server * server, int index);
-struct woe_timer * woe_timer_free(struct woe_timer * me);
-int woe_timer_get(struct woe_timer * me, double * value, double * interval);
-int woe_timer_set(struct woe_timer * me, double value, double interval);
-int woe_timer_passed(struct woe_timer * me);
-int woe_timer_callback(struct woe_timer * me);
-
-#endif

+ 0 - 23
include/toruby.h

@@ -1,23 +0,0 @@
-#ifndef toruby_H_INCLUDED
-#define toruby_H_INCLUDED
-
-#include <mruby.h>
-
-/* This file was generated with:
-'cfunctions -c -aoff -n -w toruby_proto src/toruby.c' */
-#ifndef CFH_TORUBY_PROTO
-#define CFH_TORUBY_PROTO
-
-/* From 'src/toruby.c': */
-
-int tr_init (mrb_state * mrb );
-
-#endif /* CFH_TORUBY_PROTO */
-#include "rh.h"
-
-
-#endif
-
-
-
-

+ 0 - 30
include/tr_file.h

@@ -1,30 +0,0 @@
-#ifndef FILE_H_INCLUDED
-#define FILE_H_INCLUDED
-
-#include <stdlib.h>
-#include <stdarg.h>
-
-#include "config.h"
-
-struct woe_file;
-typedef struct woe_file woe_file;
-
-woe_file * woe_file_open(struct woe_config * cfg, char * filename, char * mode);
-void woe_file_close(woe_file * file);
-
-size_t woe_file_write(woe_file * file, void * buf, size_t size);
-size_t woe_file_read(woe_file * file, void * buf, size_t size);
-
-int woe_file_puts(woe_file * file, char * buf);
-int woe_file_gets(woe_file * file, char * buf, int size);
-
-int woe_file_putc(woe_file * file, char c);
-int woe_file_getc(woe_file * file, char c);
-
-int woe_mkdir(struct woe_config * cfg, char * filename);
-
-int woe_file_eof(woe_file * file);
-
-int tr_init_file(mrb_state * mrb);
-
-#endif

+ 0 - 151
include/tr_macro.h

@@ -1,151 +0,0 @@
-/*
- * This file contains macros to help generate the bindings
- * of functionality to MRuby.
- */
-
-#ifndef TR_MACRO_H_INCLUDED
-#define TR_MACRO_H_INCLUDED
-
-#define RH_WRAP(RUBY, TYPE, OBJECT)                         \
-mrb_obj_value(Data_Wrap_Struct(RUBY,                        \
-              RUBY->object_class, &toruby_##TYPE, OBJECT));
-
-#define TR_NAME(NAME)               tr_##NAME
-#define TR_PAIR_HELPER(NAME)        TR_NAME(NAME), NAME
-#define TR_PAIR(NAME)               TR_PAIR_HELPER(NAME)
-#define TR_MACRO_AID(MACRO, ARGS)   MACRO ARGS        
-#define TR_PAIR_DO_AID(MACRO, NAME) TR_MACRO_AID(MACRO, (TR_PAIR(NAME)))
-#define TR_PAIR_DO(MACRO, NAME)     TR_PAIR_DO_AID(MACRO, NAME)
-
-#define TORUBY_0_ICALLER(NAME, TOCALL)                                         \
-  static mrb_value NAME(mrb_state * mrb, mrb_value self) {                     \
-  (void) self; (void) mrb;                                                     \
-  return mrb_fixnum_value(TOCALL());                                           \
-}
-
-#define TORUBY_0_FGETTER(NAME, TOCALL)                                         \
-  static mrb_value NAME(mrb_state * mrb, mrb_value self) {                     \
-  (void) self;                                                                 \
-  return mrb_float_value(mrb, TOCALL());                                       \
-}
-
-#define TR_WRAP_NOARG_BOOL(NAME, TOCALL)                                       \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {                       \
-  (void) self; (void) mrb;                                                     \
-  return rh_bool_value(TOCALL());                                              \
-}
-
-#define TR_WRAP_I_BOOL(NAME, TOCALL)                                           \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {                       \
-  mrb_int i1;                                                                  \
-  (void) self;                                                                 \
-  mrb_get_args(mrb, "i", &i1);                                                 \
-  return rh_bool_value(TOCALL(i1));                                            \
-}
-
-#define TR_WRAP_B_BOOL(NAME, TOCALL)                                           \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {                       \
-  mrb_value b1;                                                                \
-  (void) self;                                                                 \
-  mrb_get_args(mrb, "o", &b1);                                                 \
-  return rh_bool_value(TOCALL(rh_tobool(b1)));                                 \
-}
-
-#define TR_WRAP_IZ_INT(NAME, TOCALL)                                           \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {                       \
-  mrb_int i1 = -1;                                                             \
-  char * str = NULL;                                                           \
-  (void) self;                                                                 \
-  mrb_get_args(mrb, "iz", &i1, &str);                                          \
-  return mrb_fixnum_value(TOCALL(i1, str));                                    \
-}
-
-
-#define TR_WRAP_I_INT(NAME, TOCALL)                                            \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {                       \
-  mrb_int i1;                                                                  \
-  mrb_get_args(mrb, "i", &i1);                                                 \
-  (void) self;                                                                 \
-  return mrb_fixnum_value(TOCALL(i1));                                         \
-}
-
-#define TR_WRAP_II_INT(NAME, TOCALL)                                           \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {                       \
-  mrb_int i1, i2;                                                              \
-  mrb_get_args(mrb, "ii", &i1, &i2);                                           \
-  (void) self;                                                                 \
-  return mrb_fixnum_value(TOCALL(i1, i2));                                     \
-}
-
-#define TR_WRAP_III_INT(NAME, TOCALL)                                          \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {                       \
-  mrb_int i1, i2, i3;                                                          \
-  mrb_get_args(mrb, "iii", &i1, &i2, &i3)                                      \
-  (void) self;                                                                 \
-  return mrb_fixnum_value(TOCALL(i1, i2, i3));                                 \
-}
-
-
-#define TR_WRAP_IIII_INT(NAME, TOCALL)                                         \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {                       \
-  mrb_int i1, i2, i3, i4;                                                      \
-  mrb_get_args(mrb, "iiii", &i1, &i2, &i3, &i4);                               \
-  (void) self;                                                                 \
-  return mrb_fixnum_value(TOCALL(i1, i2, i3, i4));                             \
-}
-
-#define TR_WRAP_IIIIB_INT(NAME, TOCALL)                                        \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {                       \
-  mrb_int i1, i2, i3, i4;                                                      \
-  mrb_value b5;                                                                \
-  (void) self;                                                                 \
-  mrb_get_args(mrb, "iiiib", &i1, &i2, &i3, &i4, &b5);                         \
-  return mrb_fixnum_value(TOCALL(i1, i2, i3, i4, rh_tobool(b5)));              \
-}
-
-
-#define TR_WRAP_NOARG_INT(NAME, TOCALL)                                        \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {                       \
-  (void) self; (void) mrb;                                                     \
-  return mrb_fixnum_value(TOCALL());                                           \
-}
-
-
-
-#define TR_METHOD(MRB, CLASS, NAME, IMPL, FLAGS)                               \
-        mrb_define_method((MRB), (CLASS), (NAME), (IMPL), (FLAGS))
-
-#define TR_METHOD_ARGC(MRB, CLASS, NAME, IMPL, ARGC)\
-        mrb_define_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC))
-
-#define TR_METHOD_NOARG(MRB, CLASS, NAME, IMPL)\
-        mrb_define_method((MRB), (CLASS), (NAME), (IMPL), ARGS_NONE())
-
-#define TR_METHOD_OPTARG(MRB, CLASS, NAME, IMPL, ARGC, OPTC) \
-mrb_define_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC) | ARGS_OPT(OPTC))
-
-#define TR_CLASS_METHOD(MRB, CLASS, NAME, IMPL, FLAGS)\
-        mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), (FLAGS))
-
-#define TR_CLASS_METHOD_ARGC(MRB, CLASS, NAME, IMPL, ARGC)\
-        mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC))
-
-#define TR_CLASS_METHOD_NOARG(MRB, CLASS, NAME, IMPL)\
-        mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), ARGS_NONE())
-
-#define TR_CLASS_METHOD_OPTARG(MRB, CLASS, NAME, IMPL, ARGC, OPTC) \
-mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC) | ARGS_OPT(OPTC))
-
-
-
-#define TR_CONST_INT(MRB, CLASS, NAME, VALUE) \
-      mrb_define_const((MRB), (CLASS), (NAME), mrb_fixnum_value(VALUE))
-
-#define TR_CONST_INT_EASY(MRB, CLASS, PREFIX, NAME) \
-        TR_CONST_INT(MRB, CLASS, #NAME, PREFIX##NAME)
-
-#define TR_CONST_INT_VALUE(MRB, CLASS, VALUE) \
-        TR_CONST_INT(MRB, CLASS, #VALUE, VALUE)
-
-
-#endif // TR_H_INCLUDED

+ 0 - 58
oldsrc/callrb.c

@@ -1,58 +0,0 @@
-
-/* Callbacks from the C side into the mruby side.
- * Used to signal several events such as collisions or sprite
- * animation.
-*/
-#include "state.h"
-#include "rh.h"
-#include "spritestate.h"
-#include "callrb.h"
-
-/* Sprite event handler. Calls an mruby callback. */
-int callrb_sprite_event(SpriteState * spritestate, int kind, void * data) { 
-  mrb_value res;
-  Sprite * sprite;
-  State * state;
-  int spriteid, thingid, pose, direction;
-  Ruby * ruby;
-  void * thing;
-  sprite    = spritestate_sprite(spritestate);
-  spriteid  = sprite_id(sprite);
-  thing     = spritestate_data(spritestate);  
-  pose      = spritestate_pose(spritestate);
-  direction = spritestate_direction(spritestate);
-  state     = state_get();
-  ruby      = state_ruby(state);
-  res       = rh_run_toplevel(ruby, "eruta_on_sprite", "iiii", 
-                spriteid, pose, direction, kind);
-  (void) data;
-  return rh_tobool(res);
-}
-
-/* Calls the eruta_on_start function  */ 
-int callrb_on_start() { 
-  mrb_value res;
-  State * state = state_get();
-  Ruby * ruby   = state_ruby(state);
-  res           = rh_run_toplevel(ruby, "woe_on_start", "");
-  return rh_tobool(res);
-}
-
-/* Calls the eruta_on_reload function  */
-int callrb_on_reload() { 
-  mrb_value res;
-  State * state = state_get();
-  Ruby * ruby   = state_ruby(state);
-  res           = rh_run_toplevel(ruby, "woe_on_reload", "");
-  return rh_tobool(res);
-}
-
-
-/* Calls the eruta_on_update function. */
-int callrb_on_update(mrb_ruby * self, double dt) {
-  mrb_value res;  
-  mrb_value mval = mrb_float_value(self, dt);
-  res = rh_run_toplevel_args(state_ruby(self), "woe_on_update", 1, &mval);
-  return rh_tobool(res);
-}
-

+ 0 - 227
oldsrc/client.c

@@ -1,227 +0,0 @@
-/**
- * This file client.c, handles clients of the WOE server.
- */
-
-#define _POSIX_C_SOURCE 200801L
-#define _POSIX_SOURCE 200801L
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <poll.h>
-#include <unistd.h>
-
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <ctype.h>
-
-
-#include "libtelnet.h"
-#include "monolog.h"
-#include "rh.h"
-#include "client.h"
-#include "server.h"
-
-
-
-struct woe_client * woe_client_alloc() {
-  return calloc(sizeof(struct woe_client), 1);
-}
-
-struct woe_client * woe_client_init(struct woe_client * client,
-  struct woe_server * server, int index, int socket, 
-  struct sockaddr_in *addr, socklen_t addrlen) {
-  if (!client) return NULL;
-  client->server  = server;
-  client->sock    = socket;
-  client->addr    = *addr;
-  client->addrlen = addrlen;
-  client->busy    = !0;
-  return client;
-}
-
-struct woe_client * woe_client_new(struct woe_server * server, int index, int socket, 
-  struct sockaddr_in *addr, socklen_t addrlen) {
-  struct woe_client * client = woe_client_alloc();  
-  return woe_client_init(client, server, index, socket, addr, addrlen);
-}
-
-struct woe_client * woe_client_done(struct woe_client * client) {
-  /* Do nothing yet, refactor later. */
-  if (!client) return NULL;
-  if (client->telnet) telnet_free(client->telnet);
-  client->sock   = -1;
-  client->telnet = NULL;
-  LOG_NOTE("Connection to client %d closed.\n", client->index);
-  client->index = -1;
-  return client;
-}
-
-
-struct woe_client * woe_client_free(struct woe_client * client) {
-  woe_client_done(client);
-  free(client);  
-  return NULL;
-}
-
-
-int woe_client_send(struct woe_client * client, const char *buffer, unsigned int size) {
-  int res;
-  if (!client) return 1;
-  /* ignore on invalid socket */
-  if (client->sock < 0)  return 2;
-  
-  /* send data */
-  while (size > 0) {
-    if ((res = send(client->sock, buffer, size, 0)) == -1) {
-      if (errno != EINTR && errno != ECONNRESET) {
-        LOG_ERROR("send() failed: %s\n", strerror(errno));
-        return 3;
-      } else {
-        return 0;
-      }
-    } else if (res == 0) {
-      LOG_ERROR("send() unexpectedly returned 0\n");
-      return 4;
-    }
-
-    /* Update pointer and size to see if we've got more to send */
-    buffer += res;
-    size   -= res;
-  }
-  
-  return 0;
-}
-
-int woe_client_input(struct woe_client * cli, const char *buffer, size_t size) {
-  mrb_state * mrb;
-  LOG_NOTE("Received input for client %d\n", cli->index);
-  mrb = woe_server_get_mrb(cli->server);  
-  if (mrb) {  
-    rh_run_toplevel(mrb, "woe_on_input", "is", cli->index, buffer, size);
-  }
-  return 0;
-}
-
-int woe_client_zmp(struct woe_client * cli, int argc, const char *argv[]) {
-  unsigned int i;
-  mrb_state * mrb;
-  LOG_NOTE("Received ZMP reply for client %d\n", cli->index);
-  mrb = woe_server_get_mrb(cli->server);  
-  if (mrb) {  
-    rh_run_toplevel(mrb, "woe_begin_zmp", "ii", cli->index, argc);
-    for (i=0; i < argc; i++) {
-      rh_run_toplevel(mrb, "woe_zmp_arg", "iiz", cli->index, i, argv[i]);
-    }  
-    rh_run_toplevel(mrb, "woe_finish_zmp", "ii", cli->index, argc);
-  }
-  return 0;
-}
-
-int woe_client_iac(struct woe_client * cli, int cmd) {
-  mrb_state * mrb;
-  LOG_NOTE("Received iac for client %d %d\n", cli->index, cmd);
-  mrb = woe_server_get_mrb(cli->server);  
-  if (mrb) {  
-    rh_run_toplevel(mrb, "woe_on_iac", "i", cli->index, cmd);
-  }
-  return 0;
-}
-
-
-int woe_client_negotiate(struct woe_client * cli, int how, int option) {
-  mrb_state * mrb;
-  LOG_NOTE("Received negotiate for client %d %d %d\n", cli->index, how, option);
-  mrb = woe_server_get_mrb(cli->server);  
-  if (mrb) {  
-    rh_run_toplevel(mrb, "woe_on_negotiate", "iii", cli->index, how, option);
-  }
-  return 0;
-}
-
-int woe_client_subnegotiate(struct woe_client * cli, const char * buf, int len, int telopt) {
-  mrb_state * mrb;
-  LOG_NOTE("Received subnegotiate for client %d\n", cli->index);
-  mrb = woe_server_get_mrb(cli->server);  
-  if (mrb) {  
-    rh_run_toplevel(mrb, "woe_on_subnegotiate", "iis", cli->index, telopt, buf, len);
-  }
-  return 0;
-}
-
-int woe_client_ttype(struct woe_client * cli, int cmd, const char * name) {
-  mrb_state * mrb;
-  LOG_NOTE("Received ttype for client %d %d %s\n", cli->index, cmd, name);
-  mrb = woe_server_get_mrb(cli->server);  
-  if (mrb) {  
-    rh_run_toplevel(mrb, "woe_on_ttype", "iz", cli->index, cmd, name);
-  }
-  return 0;
-}
-
-
-int woe_client_error(struct woe_client * cli, int code, const char * msg) {
-  mrb_state * mrb;
-  LOG_NOTE("Received error for client %d %d %s\n", cli->index, code, msg);
-  mrb = woe_server_get_mrb(cli->server);  
-  if (mrb) {  
-    rh_run_toplevel(mrb, "woe_on_error", "iz\n", cli->index, code, msg);
-  }
-  return 0;
-}
-
-
-int woe_client_warning(struct woe_client * cli, int code, const char * msg) {
-  mrb_state * mrb;
-  LOG_NOTE("Received warning for client %d %d %s\n", cli->index, code, msg);
-  mrb = woe_server_get_mrb(cli->server);  
-  if (mrb) {  
-    rh_run_toplevel(mrb, "woe_on_warning", "iz", cli->index, code, msg);
-  }
-  return 0;
-}
-
-
-int woe_client_compress(struct woe_client * cli, int state) {
-  mrb_state * mrb;
-  LOG_NOTE("Received compress for client %d %d\n", cli->index, state);
-  mrb = woe_server_get_mrb(cli->server);  
-  if (mrb) {  
-    rh_run_toplevel(mrb, "woe_on_compress", "ii", cli->index, state);
-  }
-  return 0;
-}
-
-int woe_client_environ(struct woe_client * cli, int cmd, const struct telnet_environ_t *values, size_t size) {
-  int i;
-  mrb_state * mrb;
-  LOG_NOTE("Received environ for client %d %d\n", cli->index, cmd);
-  mrb = woe_server_get_mrb(cli->server);  
-  if (mrb) {  
-    rh_run_toplevel(mrb, "woe_begin_environ", "iii", cli->index, cmd, size);
-    for (i=0; i < size; i++) {
-      rh_run_toplevel(mrb, "woe_environ_arg", "iiizz", cli->index, i, values[i].type, values[i].var, values[i].value);
-    }  
-   rh_run_toplevel(mrb, "woe_finish_environ", "iii", cli->index, cmd, size);
-  }
-  return 0;
-}
-
-int woe_client_mssp(struct woe_client * cli, const struct telnet_environ_t *values, size_t size) {
-  int i;
-  mrb_state * mrb;
-  LOG_NOTE("Received mssp for client %d\n", cli->index);
-  mrb = woe_server_get_mrb(cli->server);  
-  if (mrb) {  
-    rh_run_toplevel(mrb, "woe_begin_mssp", "ii", cli->index, size);
-    for (i=0; i < size; i++) {
-      rh_run_toplevel(mrb, "woe_mssp_arg", "iiizz", cli->index, i, values[i].type, values[i].var, values[i].value);
-    }  
-   rh_run_toplevel(mrb, "woe_finish_mssp", "iii", cli->index, size);
-  }
-  return 0;
-}
-

+ 0 - 61
oldsrc/config.c

@@ -1,61 +0,0 @@
-#include "config.h"
-
-#define _POSIX_C_SOURCE 200801L
-#define _POSIX_SOURCE 200801L
-
-#include <stdlib.h>
-#include <unistd.h>
-
-
-
-
-int woe_config_init_args(struct woe_config * config, int argc, char * argv[]) {
-  char opt;
-  
-  config->port      = 7777;
-  config->data_dir  = getenv("WOE_DATA");
-  if (!config->data_dir) {
-    config->data_dir= "data";
-  }
-  
-  config->log_file  = getenv("WOE_LOG");
-  if (!config->log_file) {
-    config->log_file = "woe.log";
-  }
-    
-  while ((opt = getopt(argc, argv, "p:d:l:")) != -1) {
-    switch (opt) {
-    case 'p':
-      config->port = atoi(optarg);
-      break;
-    
-    case 'd': 
-      config->data_dir = optarg;
-      break;
-      
-    case 'l': 
-      config->log_file = optarg;
-      break;  
-            
-    default: /* '?' */
-      break;
-    }
-  }
-  return 0;
-}
-
-
-static struct woe_config global_woe_config;
-
-struct woe_config woe_config_get() {
-  return global_woe_config;
-}
-
-struct woe_config woe_config_put(struct woe_config * config) {
-  if (config) {
-    global_woe_config = *config;
-  }  
-  return woe_config_get();
-}
-
-

+ 0 - 728
oldsrc/dynar.c

@@ -1,728 +0,0 @@
-#include "dynar.h"
-#include "mem.h"
-
-#include <stdlib.h>
-#include <string.h>
-
-#define ARRAY_ROOM 16
-
-/**
-* Dynar is a DYNamic ARray of size elements each of elsz size.
-* For simplicity, dynarr is exactly sized, that is,
-* it does not allocate any extra elements but the amount requested.
-* In other words it's capacity is equal to it's size.
-*/
-struct Dynar_ {
-  char * data;
-  // use a char * pointer since that makes pointer arithmetic easier
-  int size;
-  int elsz;
-};
-
-/** Gets the array's size. Returns 0 if self is NULL. */
-int dynar_size(Dynar * self) {
-  if(!self) return 0;
-  return self->size;
-}
-
-/** Gets the amount of elements in the vector. Returns 0 if self is NULL.
- * Alias for dynar_size(self).
- **/
-int dynar_amount(Dynar * self) {
-  if(!self) return 0;
-  return self->size;
-}
-
-/** Gets the array's element size.  Returns 0 if self is NULL. */
-int dynar_elementsize(Dynar * self) {
-  if(!self) return 0;
-  return self->elsz;
-}
-
-/** Frees the contents of an array. Has the same effect as emptying the array. Does not call a desctructor on any elements contained! */
-Dynar * dynar_done(Dynar * self) {
-  if(!self) return NULL;
-  mem_free(self->data);
-  self->data = NULL;
-  self->size = 0;
-  return self;
-}
-
-/** Calls a destructor on the contents of the array if it is not null. 
- The contents are considered to be pointers. Does not call dynar_free()!!! */
-Dynar * dynar_destroy(Dynar * self, MemDestructor * destroy) {
-  int index;
-  int size = dynar_size(self);
-  if(!self)     return self;
-  if(!destroy)  return NULL;
-  for(index = 0; index < size ; index++) {
-    void * ptr = dynar_getptr(self, index);    
-    destroy(ptr);
-  }
-  return self;
-}
-
-/** Calls a destructor on the contents of the array if it is not null. 
- The contents are considered to be structs. Does not call dynar_free()!!! */
-Dynar * dynar_destroy_structs(Dynar * self, MemDestructor * destroy) {
-  int index;
-  int size = dynar_size(self);
-  if(!self)     return self;
-  if(!destroy)  return NULL;
-  for(index = 0; index < size ; index++) {
-    void * ptr = dynar_getdata(self, index);    
-    destroy(ptr);
-  }
-  return self;
-}
-
-
-/** Frees an array. Returns NULL. */
-Dynar * dynar_free(Dynar * self) {
-  dynar_done(self);
-  return mem_free(self);
-}
-
-/** Calls a destructor on the elements of the array and then frees the array */
-Dynar * dynar_free_destroy(Dynar * self, MemDestructor * destroy) {
-  dynar_destroy(self, destroy);
-  return dynar_free(self);
-}
-
-/** Calls a destructor on the elements of the array and then frees the array */
-Dynar * dynar_destroy_structs_and_free(Dynar * self, MemDestructor * destroy) {
-  dynar_destroy_structs(self, destroy);
-  return dynar_free(self);
-}
-
-
-/** Allocates a new empty array.  */
-Dynar * dynar_alloc() {
-  return STRUCT_ALLOC(Dynar);
-}  
-
-
-/** Initializes an empty array with 0 elements of size elsz each. */
-Dynar * dynar_initempty(Dynar * self, int elsz) {
-  if(!self)    return NULL;
-  if(elsz < 0) return NULL;
-  self->size = 0;
-  self->elsz = elsz;
-  return self;
-}
-
-/** Allocates a new empty array for elements of size elsz each. */
-Dynar * dynar_newempty(int elsz) {
-  Dynar * res = dynar_alloc();
-  if(!dynar_initempty(res, elsz)) {
-    return dynar_free(res);
-  }
-  return res;
-}
-
-/** Changes the size of the dynamic array. Newsize must be >= 1. */
-Dynar * dynar_size_(Dynar* self, int newsize) {
-  int elsz = dynar_elementsize(self);
-  void * newd = NULL;
-  int delta;
-  if(!self) return NULL;
-  // don't allow a newsize of 0, since that will make realloc call
-  // free on self->data, which we don't want.
-  if(newsize < 1) return NULL;
-  newd = mem_realloc(self->data, newsize * elsz);
-  if(!newd) return NULL;
-  // if we get here realloc was successful, so it should be safe to reassign
-  // self->data
-  self->data = newd;
-  // now, empty the unused new data, but only if growing! (XXX: is this ok???) 
-  delta =  newsize - self->size;
-  if(delta > 0) { 
-    memset(self->data + (self->size * self->elsz), 0, (delta * self->elsz));
-  }
-  self->size = newsize;  
-  return self;
-}
-
-/** Initializes a new array with a amount elements of size
-elsz each. */
-Dynar * dynar_init(Dynar * self, int amount, int elsz) {
-  Dynar * aid = dynar_initempty(self, elsz);
-  if(!aid) return NULL;
-  if(!dynar_size_(self, amount)) return NULL;
-  return self;
-}
-
-/** Initializes a new array with a capacity to store amount void* pointers.*/
-Dynar * dynar_initptr(Dynar * self, int amount) {
-  return dynar_init(self, amount, sizeof(void *));
-}
-
-/** Allocates a new array with amount elements of size elsz each. */
-Dynar * dynar_new(int amount, int elsz) {
-  Dynar * res = dynar_alloc();
-  if(!dynar_init(res, amount, elsz)) {
-      return dynar_free(res);
-  }
-  return res;
-}
-
-
-/** Allocates a new array that will be able to contain amount void * pointers.*/
-Dynar * dynar_newptr(int amount) {
-  return dynar_new(amount, sizeof(void *));
-}
-
-
-/** Sets the amount of elements of the the array, but ony if
-* amount bigger than the bigger than the current size.
-* Returns NULL if the array was not grown, otherwise returns self.
-*/
-Dynar * dynar_grow(Dynar * self, int amount) {
-  int mysize = dynar_size(self);
-  if (mysize >= amount) return NULL;
-  return dynar_size_(self, amount);
-}
-
-/** Checks if the index is smaller than the array's available room . */
-int dynar_sizeindex_ok(Dynar * self, int index) {
-  if(!self) return FALSE;
-  return index < dynar_size(self);
-}
-
-/** Checks if the int index is smaller than the array's available room. */
-int dynar_index_ok(Dynar * self, int index) {
-  if(!self) return FALSE;
-  if (index < 0) return FALSE;
-  return dynar_sizeindex_ok(self, (int)(index));
-}
-
-/** Returns a pointer to the index-th element of the array.
-Does no bounds checking! */
-void * dynar_getraw_unsafe(Dynar * self, int index) {
-  return self->data + (index * self->elsz);
-}
-
-/** Copies dynar_elementsize(self) of bytes from the index-th element
-of the array to out, which must be pointing to a bufer of at least
-dynar_elementsize(self). Does no bounds checking!
-Returns NULL on failure, out on success.
-*/
-void * dynar_getcopy_unsafe(Dynar * self, int index, void * out) {
-  char * cptr = (char *) dynar_getraw_unsafe(self, index);
-  int size = dynar_elementsize(self);
-  if((!self) || (!out) || (!cptr)) return NULL;
-  mem_move(out, cptr, size);
-  return out;
-}
-
-/** Copies dynar_elementsize(self) of bytes from the data pointed to
-* by ptr into the location pointed to by index.
-* Does no bounds checking!
-*/
-Dynar * dynar_putraw_unsafe(Dynar * self, int index, void * ptr) {
-  char * cptr = (char *) dynar_getraw_unsafe(self, index);
-  int size = dynar_elementsize(self);
-  if((!self) || (!ptr) || (!cptr)) return NULL;
-  mem_move(cptr, ptr, size);
-  return self;
-}
-
-/** Returns a pointer to the index-th element of the array.
-Does bounds checking and return NULL if out of bounds */
-void * dynar_getraw(Dynar * self, int index) {
-  // Bounds check
-  if(!dynar_index_ok(self, index)) { return NULL; }
-  return dynar_getraw_unsafe(self, index);
-}
-
-/** Returns a pointer to the index-th element of the array.
-Does bounds checking and return NULL if out of bounds */
-void * dynar_getcopy(Dynar * self, int index, void * ptr) {
-  // Bounds check
-  if(!dynar_index_ok(self, index)) { return NULL; }
-  return dynar_getcopy_unsafe(self, index, ptr);
-}
-
-/** Copies the dynar_elementsize(self) of bytes from the data pointed to
-* by ptr into the location pointed to by index.
-* Does bounds checking and return NULL if ouut of bounds.
-*/
-Dynar * dynar_putraw(Dynar * self, int index, void * ptr) {
-  // Bounds check
-  if(!dynar_index_ok(self, index)) { return NULL; }
-  return dynar_putraw_unsafe(self, index, ptr);
-}
-
-/** Stores a pointer at the index of the array.
-* Does bounds checking. dynar_elementsize(self) sould have been
-* initialized as sizeof(void *) by using dynar_newptr
-*/
-void * dynar_putptr(Dynar * self, int index, void * ptr) {
-  return dynar_putraw(self, index, &ptr);
-  // use &ptr because we want to put the contents of the pointer,
-  // not the pointer itself. 
-}
-
-/** Returns a pointer that was stored at the index index of the array. */
-void * dynar_getptr(Dynar * self, int index) {
-  void * res = NULL; 
-  if(!dynar_getcopy(self, index, &res)) return NULL;
-  // use &ptr because we want to fetch the contents of the pointer,
-  // which will be copied from the element of the array.
-  return res;
-}
-
-/** Copies the element that *ptr points to into this array at position
-index */
-void * dynar_putdata(Dynar * self, int index, void * ptr) {
-  return dynar_putraw(self, index, ptr);
-}
-
-/** Returns a pointer to the index-th element of the array. */
-void * dynar_getdata(Dynar * self, int index) {
-  return dynar_getraw(self, index);
-}
-
-/* Applies quick sort to the array using the given comparator. */
-Dynar * dynar_qsort(Dynar * self, DynarCompare  * compare) {
-  void * base; int nmemb; size_t size;
-  if(!self) return NULL;
-  base  = self->data;
-  nmemb = self->size;
-  size  = self->elsz;
-  qsort(base, nmemb, size, compare);
-  return self;
-}
-
-/* Applies a binary search to the array using the given comparator. 
- User must call dynar_qsort first. */
-void * dynar_bsearch(Dynar * self, const void * key, DynarCompare  * compare) {
-  void * base; int nmemb; size_t size;
-  if (!self) return NULL;
-  base  = self->data;
-  nmemb = self->size;
-  size  = self->elsz;
-  return bsearch(key, base, nmemb, size, compare);
-}
-
-/* Puts NULL in every element of this array using dynar_putptr */
-Dynar * dynar_putnullall(Dynar * self) {
-  int stop = dynar_size(self);
-  int index;
-  for (index = 0; index < stop; index++) {
-    dynar_putptr(self, index, NULL);
-  }
-  return self; 
-}
-
-
-
-/* Iterator helper: fill in every->now as data. */
-Every * dynar_everynow_data(Every * every) {
-  every->now   = dynar_getdata(every->on, every->index);
-  if(every->now) return every;
-  return NULL;
-}
-
-/* Iterator helper: fill in every->now as pointer. */
-Every * dynar_everynow_ptr(Every * every) {
-  every->now   = dynar_getptr(every->on, every->index);
-  if(every->now) return every;
-  return NULL;
-}
-
-/* Iterator helpers: init */
-Every  * dynar_everyinit_data(Every * every) {
-  every->index = 0;
-  return dynar_everynow_data(every);
-}
-
-/* Iterator helpers: next */
-Every  * dynar_everynext_data(Every * every) {
-  every->index++;
-  return dynar_everynow_data(every);
-}
-
-/* Iterator helpers: put. */
-void  * dynar_everyput_data(Every * every, void * data) {
-  return dynar_putdata(every->on, every->index, data);
-}
-
-/* Iterator helpers: done. */
-void  * dynar_everydone(Every * every) {
-  return every;
-}
-
-/* Iterator helpers: init pointers */
-Every  * dynar_everyinit_ptr(Every * every) {
-  every->index = 0;
-  return dynar_everynow_ptr(every);
-}
-
-/* Iterator helpers: next pointers */
-Every  * dynar_everynext_ptr(Every * every) {
-  every->index++;
-  return dynar_everynow_ptr(every);
-}
-
-/* Iterator helpers: put pointers. */
-void  * dynar_everyput_ptr(Every * every, void * data) {
-  return dynar_putptr(every->on, every->index, data);
-}
-
-
-/* Iterator helper table. */
-static EveryActs dynar_every_data_acts_ = {
-  dynar_everydone,
-  dynar_everyinit_data,
-  dynar_everynext_data,
-  dynar_everyput_data  
-};
-
-/* Iterator helper table. */
-static EveryActs dynar_every_ptr_acts_ = {
-  dynar_everydone,
-  dynar_everyinit_ptr,
-  dynar_everynext_ptr,
-  dynar_everyput_ptr
-};
-
-
-/** Iterates over the data. Call every_free when done. */
-Every * dynar_every_data(Dynar * dynar) {
-  return every_new(&dynar_every_data_acts_);
-}
-
-/** Iterates over the pointers in this array. Call every_free when done. */
-Every * dynar_every_ptr(Dynar * dynar) {
-  return every_new(&dynar_every_ptr_acts_);
-}
-
-
-/* Walks over the array using the each interface, accessing
-the data as pointers. eachdo should return non-null to break the iteration,
-the data thus found is returned bu this function. */
-void * dynar_each_ptr(Dynar * self, EachDo * eachdo, void * extra) {
-  Each each;
-  int index;
-  int size = dynar_size(self);  
-  each_init(&each, self, extra); 
-  for(index = 0; index < size ; index++) {
-    void * aid;
-    void * ptr = dynar_getptr(self, index);    
-    each_next(&each, ptr);
-    aid = eachdo(&each);
-    if (aid) return aid;
-  }
-  return NULL;
-}
-
-/* Walks over the array using the Each interface, accessing
-the data as stored structs. */
-void * dynar_each_data(Dynar * self, EachDo * eachdo, void * extra) {
-  Each each;
-  int index;
-  int size = dynar_size(self);
-  each_init(&each, self, extra);
-  for(index = 0; index < size ; index++) {
-    void * aid;
-    void * ptr = dynar_getdata(self, index);
-    each_next(&each, ptr);
-    aid = eachdo(&each);
-    if (aid) return aid;
-  }
-  return NULL;
-}
-
-/* Walks over the array using the walker interface, accessing
-the data as stored pointers. */
-void * dynar_walkptrbetween(Dynar * self, int low, int high, 
-                            Walker * walker, void * extra) {
-  int index;
-  int size = dynar_size(self);
-  low  = (low < 0)      ?  0   : low;
-  high = (high > size)  ? size : high;
-  for(index = low; index < high ; index++) {
-    void * aid;
-    void * ptr = dynar_getptr(self, index);
-    aid        = walker(ptr, extra);
-    if (aid) return aid;
-  }
-  return NULL;
-}
-
-/* Walks over the array using the walker interface, accessing
-the data as stored structs. */
-void * dynar_walkptr(Dynar * self, Walker * walker, void * extra) {
-  return dynar_walkptrbetween(self, 0, dynar_size(self), walker, extra);
-}
-
-/* Walks over the array using the walker interface, accessing
-the data as stored pointers. */
-void * dynar_walkdatabetween(Dynar * self, int low, int high,
-                             Walker * walker, void * extra) {
-  int index;
-  int size = dynar_size(self);
-  low  = (low < 0)      ?  0   : low;
-  high = (high > size)  ? size : high;
-  for(index = low; index < high ; index++) {
-    void * aid;
-    void * ptr = dynar_getdata(self, index);
-    aid        = walker(ptr, extra);
-    if (aid) return aid;
-  }
-  return NULL;
-}
-
-/* Walks over the array using the walker interface, accessing
-the data as stored structs. */
-void * dynar_walkdata(Dynar * self, Walker * walker, void * extra) {
-  return dynar_walkdatabetween(self, 0, dynar_size(self), walker, extra);
-}
-
-/* Walks over the array updating it, using the walker interface, accessing
-the data as stored pointers. */
-void * dynar_walkmapptrbetween(Dynar * self, int low, int high, 
-                            Walker * walker, void * extra) {
-  int index;
-  int size = dynar_size(self);
-  low  = (low < 0)      ?  0   : low;
-  high = (high > size)  ? size : high;
-  for(index = low; index < high ; index++) {
-    void * aid;
-    void * ptr = dynar_getptr(self, index);
-    aid        = walker(ptr, extra);
-    dynar_putptr(self, index, ptr);
-  }
-  return NULL;
-}
-
-/* Walks over the array updating it using the walker interface, accessing
-the data as stored structs. */
-void * dynar_walkmapptr(Dynar * self, Walker * walker, void * extra) {
-  return dynar_walkmapptrbetween(self, 0, dynar_size(self), walker, extra);
-}
-
-/* Resizes a dynar filled with pointers, and if the dynar shrinks, calls the 
- * given destructor on all elements that are to be removed. Returns self on success 
- * and NULL on failure. Even in this case, the superfluous elements 
-  fmay have been removed. */
-Dynar * dynar_resize(Dynar * self, int newsize, MemDestructor * destroy) {
-  int index; 
-  int last;
-  if (!self) return NULL;
-  last = dynar_size(self);
-  for(index = newsize; index < last; index ++) {
-     void * aid = dynar_getptr(self, index);
-     dynar_putptr(self, index, destroy(aid));
-  }
-  return dynar_size_(self, newsize);
-}
-
-
-
-/** Makes a new empty dynar to contain long integers  */
-Dynar * dynar_new_long() {
-  return dynar_newempty(sizeof(long));
-}
-
-/** Stores a long integer into a dynar (preferrably created with dynar_new_long */
-Dynar * dynar_put_long(Dynar * self, int index, long value) {
-  return dynar_putdata(self, index, &value);
-}
-
-/** Gets a long integer from a dynar */
-Dynar * dynar_get_long(Dynar * self, int index, long * value) {
-  return dynar_getcopy(self, index, value);
-}
-
-/** Grows the size of the array by 1 and appends the long in the last position */
-Dynar * dynar_append_long(Dynar * self, long value) {
-  int pos = dynar_size(self);
-  if (!dynar_size_(self, pos+1)) return NULL;
-  return dynar_put_long(self, pos, value);  
-}
-
-
-
-
-
-
-/**
-* Lilis is a doubly Linked List that points to it's members via void pointers 
-* but does not own them  in the sense that it doesn't clean them up unless requested.
-*/
-struct Lilis_ {
-  Lilis * next;
-  Lilis * prev;
-  void  * data;
-};
-
-
-/**
-* frees the tail of a llist only. Does nothing to self or other.
-*/
-
-Lilis * lilis_freetail(Lilis * self) {
-  Lilis * next, * now;
-  if(!self) return NULL;
-  now = self->next;   // skip current node, that will be deleted in lilis_free
-  while (now) { 
-    next = now->next; // already get next one
-    mem_free(now);    // now we can safely free the current node
-    now  = next;      // set current to next one.
-  }
-  return self;
-}
-
-
-/** Frees the tail of the list pointed to by self. It does not free any of the 
-emlements contained in * data. Does not alter self->prev, and doesn't 
-free that either. */
-Lilis * lilis_done(Lilis * self ) {  
-  if(!self) return NULL;
-  lilis_freetail(self);
-  self->data = NULL;
-  self->next = NULL;
-  return self;
-}
-
-/** Frees a linked list node and it's tail. Returns NULL. */
-Lilis * lilis_free(Lilis * self) {
-  lilis_done(self);
-  return mem_free(self);
-}
-
-/** Allocates a new empty linked list.  */
-Lilis * lilis_alloc() {
-  return STRUCT_ALLOC(Lilis);
-}  
-
-
-/** Initializes a linked list node */
-Lilis * lilis_init(Lilis * self, Lilis * next, Lilis * prev, void * data) {
-  if(!self)    return NULL;  
-  self->data = data;
-  self->next = next;
-  self->prev = prev;
-  return self;
-}
-
-/** Initializes a new linked list node that points to nothing. */
-Lilis * lilis_initempty(Lilis * self) {
-  return lilis_init(self, NULL, NULL, NULL);
-}
-
-
-/** Returns a newly allocatd linked list node. */
-Lilis * lilis_new(Lilis * next, Lilis * prev, void * data) {
-  Lilis * res = lilis_alloc();
-  if(!lilis_init(res, next, prev, data)) {
-    return lilis_free(res);
-  }
-  return res;
-}
-
-/** Returns a newly allocated linked list node that points to nothing. */
-Lilis * lilis_newempty() {
-  return lilis_new(NULL, NULL, NULL);
-}
-
-/** Appends a node to the current one, inserting it if needed.
-* returns other.
-*/
-Lilis * lilis_add(Lilis * self, Lilis * other)  {
-  Lilis * oldnext = self->next;
-  if ((!self) || (!other)) return NULL;
-  if (oldnext) {
-    oldnext->prev = other;   // next one's previous becomes the inserted link    
-    other->next   = oldnext; // inserted link's next is the old next
-  }
-  // these two belowear always needed even if there is no oldnext. 
-  other->prev   = self;    // and other's previous one is self.
-  self->next    = other;   // and curent next is the inserted link
-  return other;
-}
-
-/**  Creates a new node and adds it */
-Lilis * lilis_addnew(Lilis * self, void * data) {
-  Lilis * other = lilis_new(NULL, NULL, data);
-  return lilis_add(self, other);
-}
-
-
-/** Removes the node that follows self, but does NOT call lilis_free on it. 
-* returns the removed node
-*/
-Lilis * lilis_removenext(Lilis * self) {
-  Lilis * next, * nextnext;
-  if(!self) return NULL;
-  next = self->next;
-  if(!next) return NULL;
-  nextnext = next->next;
-  if(nextnext) {
-    nextnext->prev = self;
-    self->next     = nextnext;
-  } else {
-    self->next     = NULL;
-  }
-  return next;
-}
-
-/** Removes self, modifies prev if this is needed, but does NOT call 
-* lilis_free on it. 
-* returns the removed node
-*/
-Lilis * lilis_remove(Lilis * self) {
-  Lilis * prev, * next;
-  if(!self) return NULL;
-  next = self->next;
-  prev = self->prev;
-  if(next) { // link prev node to next node, so this node is cut out. 
-    prev->next = next;
-    next->prev = prev;
-  } else { // no next node, prev also gets no next node.
-    prev->next = NULL;
-  }
-  // finally unlink self.
-  self->next = NULL;
-  self->prev = NULL;
-  return next;
-
-}
-
-/** Removes the node self, and calls lilis_free on it.
-* returns NULL
-*/
-Lilis * lilis_erase(Lilis * self) {
-  Lilis * eraseme = lilis_remove(self);
-  return lilis_free(eraseme);
-}
-
-
-
-/** Removes the node that follows self, and calls lilis_free on it.
-* returns NULL
-*/
-Lilis * lilis_erasenext(Lilis * self) {
-  Lilis * eraseme = lilis_removenext(self);
-  return lilis_free(eraseme);
-}
-
-/** Gets the next node in the list. */
-Lilis * lilis_next(Lilis * self) {
-  if(!self) return NULL;
-  return self->next;
-}
-
-/** Gets the previous  node in the list. */
-Lilis * lilis_previous(Lilis * self) {
-  if(!self) return NULL;
-  return self->prev;
-}
-
-/** Gets the data of the list node */
-void * lilis_data(Lilis * self) {
-  if(!self) return NULL;
-  return self->data;
-}

+ 0 - 360
oldsrc/esh.c

@@ -1,360 +0,0 @@
-
-#include "esh.h"
-#include <stdio.h>
-#include <string.h>
-
-/* Returns true if the string is in an unusable state, false if not. */
-int esh_bad_p(char **me, size_t * size, size_t * space) {
-  if (!me) return !0;
-  if (!*me) return !0;
-  if (!size) return !0;
-  if (!space) return !0;
-  return 0;
-}
-
-
-/* Makes the string an empty string by writing a nul character at positon 0
- * and setting size to 0. */
-char * esh_make_empty(char ** me, size_t * size, size_t * space) {
-  if (esh_bad_p(me, size, space)) return NULL;
-  if ((*space) < 1) return NULL;
-  (*me)[0] = '\0';
-  (*size)  = 0;
-  return (*me);
-}
-
-/* Ensures the string is NUL terminated. */
-char * esh_nul_terminate(char ** me, size_t * size, size_t * space) {
-  if (esh_bad_p(me, size, space)) return NULL;
-  if (((*size) + 1) > (*space)) {
-    (*me)[(*space)] = '\0';
-    (*size) = (*space) - 1;
-  }  else { 
-    (*me)[(*size)+1] = '\0';
-  }
-  return (*me);
-}
-
-
-/* allocates a new string buffer with init_space. If init_space == 0 
- *  uses 1024 in stead.*/
-char * esh_alloc(char ** me, size_t * size, size_t * space, size_t init_space) {
-  if (!me) return NULL;
-  if (!space) return NULL;
-  if (!size) return NULL;
-  if (init_space < 1) init_space = 1024; 
-  (*me) = calloc(init_space, 1);
-  (*space) = init_space;
-  if (!*me) return NULL;
-  return esh_make_empty(me, size, space);
-}
-
-/* Grows the given string's space. */
-char * esh_grow(char ** me, size_t  * size, size_t * space, size_t new_space) {
-  char * aid;
-  (void) size;
-  if (!me) return NULL;
-  if (!*me) return NULL;
-  if (new_space <= (*space)) return NULL;
-  
-  aid = realloc(*me, new_space);
-  if (!aid) return NULL;
-    
-  memset(aid + (*space), '\0', new_space - (*space));
-  (*space) = new_space;
-  (*me)    = aid;   
-  return (*me);
-} 
-
-/* Makes sure there is enough space to add amount characters. */
-char * esh_ensure_space(char ** me, size_t * size, size_t * space, size_t grow_by) {
-  if (!me) return NULL;
-  if (!*me) return NULL;
-  if (!size) return NULL;
-  if (!space) return NULL;
-  
-  if ((*space) < ((*size) + grow_by)) { 
-    if (!esh_grow(me, size, space, (*space) + grow_by + 255)) return NULL;
-  }
-  
-  return (*me);
-} 
-
-char * esh_append_char(char ** me, size_t * size, size_t * space, char ch) {
-  if (!esh_ensure_space(me, size, space, 1)) return NULL;
-    
-  (*me)[(*size)] = ch;
-  (*size) = (*size) + 1;
-  return (*me);
-}
-
-
-char * esh_append_buf(char  ** me, size_t * size, size_t * space, char * buf, size_t bufsize) {
-  if (!esh_ensure_space(me, size, space, bufsize + 1)) return NULL;
-  strncpy((*me) + (*size), buf, bufsize);
-  (*size) = (*size) + bufsize;
-  return esh_nul_terminate(me, size, space);
-}
-
-char * esh_append_cstr(char ** me, size_t * size, size_t * space, char * str) {
-  return esh_append_buf(me, size, space, str, strlen(str));
-}
-
-char * esh_init_buf(char ** me, size_t * size, size_t * space, char * buf, size_t bufsize) {
-  if (!size) return NULL;
-  if (!esh_make_empty(me, size, space)) return NULL; 
-  return esh_append_buf(me, size, space, buf, bufsize);
-} 
-
-char * esh_init_cstr(char ** me, size_t * size, size_t * space, char * buf) {
-  return esh_init_buf(me, size, space, buf, strlen(buf));
-} 
-
-char * esh_new_cstr(char **me, size_t * size, size_t * space, char * buf) {
-  size_t bufsize;
-  if (!buf) return esh_new_empty(me, size, space);
-  bufsize = strlen(buf);
-  return esh_new_buf(me, size, space, buf, bufsize);
-}
-
-
-char * esh_new_buf(char **me, size_t * size, size_t * space, char * buf, size_t bufsize) {
-  if (!esh_alloc(me, size, space, bufsize)) return NULL;
-  return esh_init_buf(me, size, space, buf, bufsize);
-}
-
-/* Creates a new string with the given space and initialies it from init. */
-char * esh_new(char **me, size_t * size, size_t * space, char * init) {
-  return esh_new_buf(me, size, space, init, strlen(init));
-}
-
-/* Creates a new empty string with enough space. */
-char * esh_new_empty(char **me, size_t * size, size_t * space) {
-  return esh_new(me, size, space, "");
-}
-
-/* Frees the string and sets it to NULL. */
-char * esh_free(char ** me, size_t * size, size_t * space) {
-  if (!me) return NULL;
-  if (!*me) return NULL;
-  free(*me);
-  *me    = NULL;
-  *size  = 0;
-  *space = 0;
-  return NULL;
-}
-
-/** Reads in a file into the buffer. */
-char * esh_read_file(char ** me, size_t * size, size_t * space, FILE * file) {
-  char aid[1024];
-  int read;
-  if (esh_bad_p(me, size, space)) return NULL;
-  if (!file) return NULL;
-  
-  while(!feof(file)) { 
-    read = fread(aid, 1, sizeof(aid), file);
-    if (read > 0) { 
-      esh_append_buf(me, size, space, aid, read);
-    }
-  }
-  return (*me);
-}
-
-/** Reads a named file into the buffer. */
-char * esh_read_filename(char ** me, size_t * size, size_t * space, char * fn) {
-  char * res;
-  FILE * file = fopen(fn, "r");
-  res = esh_read_file(me, size, space, file);
-  if (file) fclose(file);
-  return res;
-}
-
-/** Joins a NULL terminated list onto va*/
-char * esh_join_va(char ** me, size_t * size, size_t * space, va_list strings) {
-  char * next;
-  while ( (next = va_arg(strings, char *)) ) {
-    if (!esh_append_cstr(me, size, space, next)) return NULL;
-  }
-  return *me;
-}
-
-/* Joins a NULL terminated lists of strings onto me */
-char * esh_join(char ** me, size_t * size, size_t * space, ...) {
-  char * res;
-  va_list strings;
-  va_start(strings, space);
-  res = esh_join_va(me, size, space, strings);
-  va_end(strings);
-  return res;
-}
-
-char * esh_init_join_cstr_va(char ** me, size_t * size, size_t * space, char * first, va_list strings) {
-  if (!esh_init_cstr(me, size, space, first)) return NULL;
-  return esh_join_va(me, size, space, strings);
-}
-
-char * esh_init_join_cstr(char ** me, size_t * size, size_t * space, char * first, ...) 
-{
-  char * res;
-  va_list strings;
-  va_start(strings, first);
-  res = esh_init_join_cstr_va(me, size, space, first, strings);
-  va_end(strings);
-  return res;
-}
-
-
-char * esh_new_join_cstr_va(char ** me, size_t * size, size_t * space, char * first, va_list strings) {
-  if (!esh_new_cstr(me, size, space, first)) return NULL;
-  return esh_join_va(me, size, space, strings);
-}
-
-char * esh_new_join_cstr(char ** me, size_t * size, size_t * space, char * first, ...) 
-{
-  char * res;
-  va_list strings;
-  va_start(strings, first);
-  res = esh_init_join_cstr_va(me, size, space, first, strings);
-  va_end(strings);
-  return res;
-}
-
-
-
-/** Deletes n bytes */
-
-#define SWIS_EXPAND(SWIS) &((SWIS)->text), &((SWIS)->size), &((SWIS)->space)
-
-int woesb_bad_p(struct woesb * me) {
-  return (esh_bad_p(SWIS_EXPAND(me)));
-}
-
-struct woesb * woesb_make_empty(struct woesb * me) {
-  if(!esh_make_empty(SWIS_EXPAND(me))) return NULL;
-  return me;
-}
-
-
-struct woesb * woesb_nul_terminate(struct woesb * me) {
-  if(!esh_nul_terminate(SWIS_EXPAND(me))) return NULL;
-  return me;  
-}
-
-struct woesb * woesb_alloc(struct woesb * me, size_t init_space) {
-  if(!esh_alloc(SWIS_EXPAND(me), init_space)) return NULL;
-  return me;
-}
-
-struct woesb * woesb_grow(struct woesb * me, size_t new_space) {
-  if(!esh_grow(SWIS_EXPAND(me), new_space)) return NULL;
-  return me;
-}
-
-struct woesb * woesb_ensure_space(struct woesb * me, size_t grow_by) {
-  if(!esh_ensure_space(SWIS_EXPAND(me), grow_by)) return NULL;
-  return me;
-}
-
-
-struct woesb * woesb_append_char(struct woesb * me, char ch) {
-  if(!esh_append_char(SWIS_EXPAND(me), ch)) return NULL;
-  return me;
-}
-
-
-struct woesb * woesb_append_buf(struct woesb * me, char * buf, size_t bufsize) {
-  if(!esh_append_buf(SWIS_EXPAND(me), buf, bufsize)) return NULL;
-  return me;
-}
-
-struct woesb * woesb_append_cstr(struct woesb * me, char * str) {
-  if(!esh_append_cstr(SWIS_EXPAND(me), str)) return NULL;
-  return me;
-}
-
-struct woesb * woesb_init_buf(struct woesb * me, char * buf, size_t bufsize) {
-  if(!esh_init_buf(SWIS_EXPAND(me), buf, bufsize)) return NULL;
-  return me;
-}
-
-struct woesb * woesb_init_cstr(struct woesb * me, char * buf) {
-  if(!esh_init_cstr(SWIS_EXPAND(me), buf)) return NULL;
-  return me;
-}
-
-
-struct woesb * woesb_new_buf(struct woesb * me, char * buf, size_t bufsize) {
-  if(!esh_new_buf(SWIS_EXPAND(me), buf, bufsize)) return NULL;
-  return me;
-}
-
-struct woesb * woesb_new(struct woesb * me, char * init) {
-  if(!esh_new(SWIS_EXPAND(me), init)) return NULL;
-  return me;
-}
-
-struct woesb * woesb_new_empty(struct woesb * me) {
-  if(!esh_new_empty(SWIS_EXPAND(me))) return NULL;
-  return me;
-}
-
-struct woesb * woesb_free(struct woesb * me) {
-  esh_free(SWIS_EXPAND(me)); 
-  return NULL;
-}
-
-struct woesb * woesb_read_file(struct woesb * me, FILE * file) {
-  if(!esh_read_file(SWIS_EXPAND(me), file)) return NULL;
-  return me;
-}
- 
-struct woesb * woesb_read_filename(struct woesb * me, char * fn) {
-  if(!esh_read_filename(SWIS_EXPAND(me), fn)) return NULL;
-  return me;
-}
-
-struct woesb * woesb_join_va(struct woesb * me, va_list strings) {
-  if (!esh_join_va(SWIS_EXPAND(me), strings)) return NULL;
-  return me;
-}
-
-struct woesb * woesb_join(struct woesb * me, ...) {
-  struct woesb * res;
-  va_list strings;
-  va_start(strings, me);
-  res = woesb_join_va(me, strings);
-  va_end(strings);
-  return res;
-}
-
-struct woesb * woesb_init_join_va(struct woesb * me, char * first, va_list strings) {
-  if (!esh_init_join_cstr_va(SWIS_EXPAND(me), first, strings)) return NULL;
-  return me;
-}
-
-
-struct woesb * woesb_init_join(struct woesb * me, char * first, ...) {
-  struct woesb * res;
-  va_list strings;
-  va_start(strings, first);
-  res = woesb_init_join_va(me, first, strings);
-  va_end(strings);
-  return res;  
-}
-
-
-struct woesb * woesb_new_join_va(struct woesb * me, char * first, va_list strings) {
-  if (!esh_new_join_cstr_va(SWIS_EXPAND(me), first, strings)) return NULL;
-  return me;
-}
-
-struct woesb * woesb_new_join(struct woesb * me, char * first, ...) {
-  struct woesb * res;
-  va_list strings;
-  va_start(strings, first);
-  res = woesb_new_join_va(me, first, strings);
-  va_end(strings);
-  return res;  
-}
-
-

+ 0 - 117
oldsrc/every.c

@@ -1,117 +0,0 @@
-#include "mem.h"
-#include "every.h"
-
-/** This file contains two iterator or iterator interfaces,
-namely Every and Each. Every uses a struct to
-keep track of it's state, while Each uses a callback function. */
-
-/** Allocates an uninitialized every object. */
-Every * every_alloc() {
-  return STRUCT_ALLOC(Every);
-}
-
-/** Deinitializes an every object.  */
-Every * every_done(Every * self) {
-  if(!self) return NULL;
-return self;
-}
-
-/** Frees an every object. */
-Every * every_free(Every * self) {
-  every_done(self);
-  mem_free(self);
-  return NULL;
-}
-
-/** Initializes an every object. */
-Every * every_init(Every * self, EveryActs * acts) {
-  if(!self) return NULL;
-  self->acts    = acts;
-  if(self->acts->init)
-    return self->acts->init(self);
-  return self;
-}
-
-/** Creates a new every object. */
-Every * every_new(EveryActs * acts) {
-  Every * self = every_alloc();
-  return every_init(self, acts);
-} 
-
-/** Gets the current object pointed to by the Every object. */
-void * every_get(Every * self) {
-  if(!self) return NULL;
-  return self->now;
-}
-
-/** Puts an object at the current position if possible.
-* Returns NULL if the put failed.
-*/
-void * every_put(Every * self, void * data) {
-  if(!self) return NULL;
-  if(self->acts->put) {
-    return self->acts->put(self, data);
-  }
-  return NULL;
-}
-
-/** Moves on to the next object and returns it.
-* Return NULL if no next object.
-*/
-Every * every_next(Every * self, void * data) {
-  if(self->acts->next) {
-    self->acts->next(self);
-    return every_get(self);
-  }
-  return NULL;
-}
-
-
-/** Initializes an EachElement */
-Each * each_init(Each * self, void * on, void * data) {
-  if(!self) return NULL;
-  self->on    = on;
-  self->extra = data;
-  self->now   = NULL;
-  self->index = -1;  
-  return self;
-}
-
-/** Moves on to next element, incrementing index. */
-Each * each_next(Each * self, void * now) {
-  if(!self) return NULL;
-  self->now = now;
-  self->index++;
-  return self;
-}
-
-/** Gets now pointer of each struct */
-void * each_now(Each * self) {
-  if(!self) return NULL;
-  return self->now;
-}  
-
-/** Gets on pointer of each struct */
-void * each_on(Each * self) {
-  if(!self) return NULL;
-  return self->on;
-}
-
-/** Gets extra data pointer of each struct */
-void * each_extra(Each * self) {
-  if(!self) return NULL;
-  return self->extra;
-}
-
-/** Gets index pointer of each struct, -1 if the struct is NULL. */
-int each_index(Each * self) {
-  if(!self) return -1;
-  return self->index;
-}
-
-
-
-
-
-
-

+ 0 - 1549
oldsrc/libtelnet.c

@@ -1,1549 +0,0 @@
-/*
- * libtelnet - TELNET protocol handling library
- *
- * Sean Middleditch
- * sean@sourcemud.org
- *
- * The author or authors of this code dedicate any and all copyright interest
- * in this code to the public domain. We make this dedication for the benefit
- * of the public at large and to the detriment of our heirs and successors. We
- * intend this dedication to be an overt act of relinquishment in perpetuity of
- * all present and future rights to this code under copyright law.
- */
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <stdarg.h>
-
-/* Win32 compatibility */
-#if defined(_WIN32)
-# define vsnprintf _vsnprintf
-# define __func__ __FUNCTION__
-# define ZLIB_WINAPI 1
-#endif
-
-#if defined(HAVE_ZLIB)
-# include <zlib.h>
-#endif
-
-#include "libtelnet.h"
-
-/* inlinable functions */
-#if defined(__GNUC__) || __STDC_VERSION__ >= 199901L
-# define INLINE __inline__
-#else
-# define INLINE
-#endif
-
-/* helper for Q-method option tracking */
-#define Q_US(q) ((q).state & 0x0F)
-#define Q_HIM(q) (((q).state & 0xF0) >> 4)
-#define Q_MAKE(us,him) ((us) | ((him) << 4))
-
-/* helper for the negotiation routines */
-#define NEGOTIATE_EVENT(telnet,cmd,opt) \
-	ev.type = (cmd); \
-	ev.neg.telopt = (opt); \
-	(telnet)->eh((telnet), &ev, (telnet)->ud);
-
-/* telnet state codes */
-enum telnet_state_t {
-	TELNET_STATE_DATA = 0,
-	TELNET_STATE_IAC,
-	TELNET_STATE_WILL,
-	TELNET_STATE_WONT,
-	TELNET_STATE_DO,
-	TELNET_STATE_DONT,
-	TELNET_STATE_SB,
-	TELNET_STATE_SB_DATA,
-	TELNET_STATE_SB_DATA_IAC
-};
-typedef enum telnet_state_t telnet_state_t;
-
-/* telnet state tracker */
-struct telnet_t {
-	/* user data */
-	void *ud;
-	/* telopt support table */
-	const telnet_telopt_t *telopts;
-	/* event handler */
-	telnet_event_handler_t eh;
-#if defined(HAVE_ZLIB)
-	/* zlib (mccp2) compression */
-	z_stream *z;
-#endif
-	/* RFC1143 option negotiation states */
-	struct telnet_rfc1143_t *q;
-	/* sub-request buffer */
-	char *buffer;
-	/* current size of the buffer */
-	size_t buffer_size;
-	/* current buffer write position (also length of buffer data) */
-	size_t buffer_pos;
-	/* current state */
-	enum telnet_state_t state;
-	/* option flags */
-	unsigned char flags;
-	/* current subnegotiation telopt */
-	unsigned char sb_telopt;
-	/* length of RFC1143 queue */
-	unsigned char q_size;
-};
-
-/* RFC1143 option negotiation state */
-typedef struct telnet_rfc1143_t {
-	unsigned char telopt;
-	unsigned char state;
-} telnet_rfc1143_t;
-
-/* RFC1143 state names */
-#define Q_NO 0
-#define Q_YES 1
-#define Q_WANTNO 2
-#define Q_WANTYES 3
-#define Q_WANTNO_OP 4
-#define Q_WANTYES_OP 5
-
-/* buffer sizes */
-static const size_t _buffer_sizes[] = { 0, 512, 2048, 8192, 16384, };
-static const size_t _buffer_sizes_count = sizeof(_buffer_sizes) /
-		sizeof(_buffer_sizes[0]);
-
-/* error generation function */
-static telnet_error_t _error(telnet_t *telnet, unsigned line,
-		const char* func, telnet_error_t err, int fatal, const char *fmt,
-		...) {
-	telnet_event_t ev;
-	char buffer[512];
-	va_list va;
-
-	/* format informational text */
-	va_start(va, fmt);
-	vsnprintf(buffer, sizeof(buffer), fmt, va);
-	va_end(va);
-
-	/* send error event to the user */
-	ev.type = fatal ? TELNET_EV_ERROR : TELNET_EV_WARNING;
-	ev.error.file = __FILE__;
-	ev.error.func = func;
-	ev.error.line = line;
-	ev.error.msg = buffer;
-	telnet->eh(telnet, &ev, telnet->ud);
-	
-	return err;
-}
-
-#if defined(HAVE_ZLIB)
-/* initialize the zlib box for a telnet box; if deflate is non-zero, it
- * initializes zlib for delating (compression), otherwise for inflating
- * (decompression).  returns TELNET_EOK on success, something else on
- * failure.
- */
-telnet_error_t _init_zlib(telnet_t *telnet, int deflate, int err_fatal) {
-	z_stream *z;
-	int rs;
-
-	/* if compression is already enabled, fail loudly */
-	if (telnet->z != 0)
-		return _error(telnet, __LINE__, __func__, TELNET_EBADVAL,
-				err_fatal, "cannot initialize compression twice");
-
-	/* allocate zstream box */
-	if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0)
-		return _error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal,
-				"malloc() failed: %s", strerror(errno));
-
-	/* initialize */
-	if (deflate) {
-		if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) {
-			free(z);
-			return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
-					err_fatal, "deflateInit() failed: %s", zError(rs));
-		}
-		telnet->flags |= TELNET_PFLAG_DEFLATE;
-	} else {
-		if ((rs = inflateInit(z)) != Z_OK) {
-			free(z);
-			return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
-					err_fatal, "inflateInit() failed: %s", zError(rs));
-		}
-		telnet->flags &= ~TELNET_PFLAG_DEFLATE;
-	}
-
-	telnet->z = z;
-
-	return TELNET_EOK;
-}
-#endif /* defined(HAVE_ZLIB) */
-
-/* push bytes out, compressing them first if need be */
-static void _send(telnet_t *telnet, const char *buffer,
-		size_t size) {
-	telnet_event_t ev;
-
-#if defined(HAVE_ZLIB)
-	/* if we have a deflate (compression) zlib box, use it */
-	if (telnet->z != 0 && telnet->flags & TELNET_PFLAG_DEFLATE) {
-		char deflate_buffer[1024];
-		int rs;
-
-		/* initialize z state */
-		telnet->z->next_in = (unsigned char *)buffer;
-		telnet->z->avail_in = (unsigned int)size;
-		telnet->z->next_out = (unsigned char *)deflate_buffer;
-		telnet->z->avail_out = sizeof(deflate_buffer);
-
-		/* deflate until buffer exhausted and all output is produced */
-		while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
-			/* compress */
-			if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) {
-				_error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
-						"deflate() failed: %s", zError(rs));
-				deflateEnd(telnet->z);
-				free(telnet->z);
-				telnet->z = 0;
-				break;
-			}
-
-			/* send event */
-			ev.type = TELNET_EV_SEND;
-			ev.data.buffer = deflate_buffer;
-			ev.data.size = sizeof(deflate_buffer) - telnet->z->avail_out;
-			telnet->eh(telnet, &ev, telnet->ud);
-
-			/* prepare output buffer for next run */
-			telnet->z->next_out = (unsigned char *)deflate_buffer;
-			telnet->z->avail_out = sizeof(deflate_buffer);
-		}
-
-		/* do not continue with remaining code */
-		return;
-	}
-#endif /* defined(HAVE_ZLIB) */
-
-	ev.type = TELNET_EV_SEND;
-	ev.data.buffer = buffer;
-	ev.data.size = size;
-	telnet->eh(telnet, &ev, telnet->ud);
-}
-
-/* to send bags of unsigned chars */
-#define _sendu(t, d, s) _send((t), (const char*)(d), (s))
-
-/* check if we support a particular telopt; if us is non-zero, we
- * check if we (local) supports it, otherwise we check if he (remote)
- * supports it.  return non-zero if supported, zero if not supported.
- */
-static INLINE int _check_telopt(telnet_t *telnet, unsigned char telopt,
-		int us) {
-	int i;
-
-	/* if we have no telopts table, we obviously don't support it */
-	if (telnet->telopts == 0)
-		return 0;
-
-	/* loop unti found or end marker (us and him both 0) */
-	for (i = 0; telnet->telopts[i].telopt != -1; ++i) {
-		if (telnet->telopts[i].telopt == telopt) {
-			if (us && telnet->telopts[i].us == TELNET_WILL)
-				return 1;
-			else if (!us && telnet->telopts[i].him == TELNET_DO)
-				return 1;
-			else
-				return 0;
-		}
-	}
-
-	/* not found, so not supported */
-	return 0;
-}
-
-/* retrieve RFC1143 option state */
-static INLINE telnet_rfc1143_t _get_rfc1143(telnet_t *telnet,
-		unsigned char telopt) {
-	telnet_rfc1143_t empty;
-	int i;
-
-	/* search for entry */
-	for (i = 0; i != telnet->q_size; ++i) {
-		if (telnet->q[i].telopt == telopt) {
-			return telnet->q[i];
-		}
-	}
-
-	/* not found, return empty value */
- 	empty.telopt = telopt;
-	empty.state = 0;
-	return empty;
-}
-
-/* save RFC1143 option state */
-static INLINE void _set_rfc1143(telnet_t *telnet, unsigned char telopt,
-		char us, char him) {
-	telnet_rfc1143_t *qtmp;
-	int i;
-
-	/* search for entry */
-	for (i = 0; i != telnet->q_size; ++i) {
-		if (telnet->q[i].telopt == telopt) {
-			telnet->q[i].state = Q_MAKE(us,him);
-			return;
-		}
-	}
-
-	/* we're going to need to track state for it, so grow the queue
-	 * by 4 (four) elements and put the telopt into it; bail on allocation
-	 * error.  we go by four because it seems like a reasonable guess as
-	 * to the number of enabled options for most simple code, and it
-	 * allows for an acceptable number of reallocations for complex code.
-	 */
-	if ((qtmp = (telnet_rfc1143_t *)realloc(telnet->q,
-			sizeof(telnet_rfc1143_t) * (telnet->q_size + 4))) == 0) {
-		_error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
-				"realloc() failed: %s", strerror(errno));
-		return;
-	}
-	memset(&qtmp[telnet->q_size], 0, sizeof(telnet_rfc1143_t) * 4);
-	telnet->q = qtmp;
-	telnet->q[telnet->q_size].telopt = telopt;
-	telnet->q[telnet->q_size].state = Q_MAKE(us, him);
-	telnet->q_size += 4;
-}
-
-/* send negotiation bytes */
-static INLINE void _send_negotiate(telnet_t *telnet, unsigned char cmd,
-		unsigned char telopt) {
-	unsigned char bytes[3];
-	bytes[0] = TELNET_IAC;
-	bytes[1] = cmd;
-	bytes[2] = telopt;
-	_sendu(telnet, bytes, 3);
-}
-
-/* negotiation handling magic for RFC1143 */
-static void _negotiate(telnet_t *telnet, unsigned char telopt) {
-	telnet_event_t ev;
-	telnet_rfc1143_t q;
-
-	/* in PROXY mode, just pass it thru and do nothing */
-	if (telnet->flags & TELNET_FLAG_PROXY) {
-		switch ((int)telnet->state) {
-		case TELNET_STATE_WILL:
-			NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
-			break;
-		case TELNET_STATE_WONT:
-			NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt);
-			break;
-		case TELNET_STATE_DO:
-			NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
-			break;
-		case TELNET_STATE_DONT:
-			NEGOTIATE_EVENT(telnet, TELNET_EV_DONT, telopt);
-			break;
-		}
-		return;
-	}
-
-	/* lookup the current state of the option */
-	q = _get_rfc1143(telnet, telopt);
-
-	/* start processing... */
-	switch ((int)telnet->state) {
-	/* request to enable option on remote end or confirm DO */
-	case TELNET_STATE_WILL:
-		switch (Q_HIM(q)) {
-		case Q_NO:
-			if (_check_telopt(telnet, telopt, 0)) {
-				_set_rfc1143(telnet, telopt, Q_US(q), Q_YES);
-				_send_negotiate(telnet, TELNET_DO, telopt);
-				NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
-			} else
-				_send_negotiate(telnet, TELNET_DONT, telopt);
-			break;
-		case Q_WANTNO:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_NO);
-			NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt);
-			_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-					"DONT answered by WILL");
-			break;
-		case Q_WANTNO_OP:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_YES);
-			NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
-			_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-					"DONT answered by WILL");
-			break;
-		case Q_WANTYES:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_YES);
-			NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
-			break;
-		case Q_WANTYES_OP:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO);
-			_send_negotiate(telnet, TELNET_DONT, telopt);
-			NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
-			break;
-		}
-		break;
-
-	/* request to disable option on remote end, confirm DONT, reject DO */
-	case TELNET_STATE_WONT:
-		switch (Q_HIM(q)) {
-		case Q_YES:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_NO);
-			_send_negotiate(telnet, TELNET_DONT, telopt);
-			NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt);
-			break;
-		case Q_WANTNO:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_NO);
-			NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt);
-			break;
-		case Q_WANTNO_OP:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES);
-			NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
-			break;
-		case Q_WANTYES:
-		case Q_WANTYES_OP:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_NO);
-			break;
-		}
-		break;
-
-	/* request to enable option on local end or confirm WILL */
-	case TELNET_STATE_DO:
-		switch (Q_US(q)) {
-		case Q_NO:
-			if (_check_telopt(telnet, telopt, 1)) {
-				_set_rfc1143(telnet, telopt, Q_YES, Q_HIM(q));
-				_send_negotiate(telnet, TELNET_WILL, telopt);
-				NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
-			} else
-				_send_negotiate(telnet, TELNET_WONT, telopt);
-			break;
-		case Q_WANTNO:
-			_set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q));
-			NEGOTIATE_EVENT(telnet, TELNET_EV_DONT, telopt);
-			_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-					"WONT answered by DO");
-			break;
-		case Q_WANTNO_OP:
-			_set_rfc1143(telnet, telopt, Q_YES, Q_HIM(q));
-			NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
-			_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-					"WONT answered by DO");
-			break;
-		case Q_WANTYES:
-			_set_rfc1143(telnet, telopt, Q_YES, Q_HIM(q));
-			NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
-			break;
-		case Q_WANTYES_OP:
-			_set_rfc1143(telnet, telopt, Q_WANTNO, Q_HIM(q));
-			_send_negotiate(telnet, TELNET_WONT, telopt);
-			NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
-			break;
-		}
-		break;
-
-	/* request to disable option on local end, confirm WONT, reject WILL */
-	case TELNET_STATE_DONT:
-		switch (Q_US(q)) {
-		case Q_YES:
-			_set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q));
-			_send_negotiate(telnet, TELNET_WONT, telopt);
-			NEGOTIATE_EVENT(telnet, TELNET_EV_DONT, telopt);
-			break;
-		case Q_WANTNO:
-			_set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q));
-			NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt);
-			break;
-		case Q_WANTNO_OP:
-			_set_rfc1143(telnet, telopt, Q_WANTYES, Q_HIM(q));
-			_send_negotiate(telnet, TELNET_WILL, telopt);
-			NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
-			break;
-		case Q_WANTYES:
-		case Q_WANTYES_OP:
-			_set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q));
-			break;
-		}
-		break;
-	}
-}
-
-/* process an ENVIRON/NEW-ENVIRON subnegotiation buffer
- *
- * the algorithm and approach used here is kind of a hack,
- * but it reduces the number of memory allocations we have
- * to make.
- *
- * we copy the bytes back into the buffer, starting at the very
- * beginning, which makes it easy to handle the ENVIRON ESC
- * escape mechanism as well as ensure the variable name and
- * value strings are NUL-terminated, all while fitting inside
- * of the original buffer.
- */
-static int _environ_telnet(telnet_t *telnet, unsigned char type,
-		char* buffer, size_t size) {
-	telnet_event_t ev;
-	struct telnet_environ_t *values = 0;
-	char *c, *last, *out;
-	size_t index, count;
-
-	/* if we have no data, just pass it through */
-	if (size == 0) {
-		return 0;
-	}
-
-	/* first byte must be a valid command */
-	if ((unsigned)buffer[0] != TELNET_ENVIRON_SEND &&
-			(unsigned)buffer[0] != TELNET_ENVIRON_IS && 
-			(unsigned)buffer[0] != TELNET_ENVIRON_INFO) {
-		_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-				"telopt %d subneg has invalid command", type);
-		return 0;
-	}
-
-	/* store ENVIRON command */
-	ev.environ.cmd = buffer[0];
-
-	/* if we have no arguments, send an event with no data end return */
-	if (size == 1) {
-		/* no list of variables given */
-		ev.environ.values = 0;
-		ev.environ.size = 0;
-
-		/* invoke event with our arguments */
-		ev.type = TELNET_EV_ENVIRON;
-		telnet->eh(telnet, &ev, telnet->ud);
-
-		return 1;
-	}
-
-	/* very second byte must be VAR or USERVAR, if present */
-	if ((unsigned)buffer[1] != TELNET_ENVIRON_VAR &&
-			(unsigned)buffer[1] != TELNET_ENVIRON_USERVAR) {
-		_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-				"telopt %d subneg missing variable type", type);
-		return 0;
-	}
-
-	/* ensure last byte is not an escape byte (makes parsing later easier) */
-	if ((unsigned)buffer[size - 1] == TELNET_ENVIRON_ESC) {
-		_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-				"telopt %d subneg ends with ESC", type);
-		return 0;
-	}
-
-	/* count arguments; each valid entry starts with VAR or USERVAR */
-	count = 0;
-	for (c = buffer + 1; c < buffer + size; ++c) {
-		if (*c == TELNET_ENVIRON_VAR || *c == TELNET_ENVIRON_USERVAR) {
-			++count;
-		} else if (*c == TELNET_ENVIRON_ESC) {
-			/* skip the next byte */
-			++c;
-		}
-	}
-
-	/* allocate argument array, bail on error */
-	if ((values = (struct telnet_environ_t *)calloc(count,
-			sizeof(struct telnet_environ_t))) == 0) {
-		_error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
-				"calloc() failed: %s", strerror(errno));
-		return 0;
-	}
-
-	/* parse argument array strings */
-	out = buffer;
-	c = buffer + 1;
-	for (index = 0; index != count; ++index) {
-		/* remember the variable type (will be VAR or USERVAR) */
-		values[index].type = *c++;
-
-		/* scan until we find an end-marker, and buffer up unescaped
-		 * bytes into our buffer */
-		last = out;
-		while (c < buffer + size) {
-			/* stop at the next variable or at the value */
-			if ((unsigned)*c == TELNET_ENVIRON_VAR ||
-					(unsigned)*c == TELNET_ENVIRON_VALUE ||
-					(unsigned)*c == TELNET_ENVIRON_USERVAR) {
-				break;
-			}
-
-			/* buffer next byte (taking into account ESC) */
-			if (*c == TELNET_ENVIRON_ESC) {
-				++c;
-			}
-
-			*out++ = *c++;
-		}
-		*out++ = '\0';
-
-		/* store the variable name we have just received */
-		values[index].var = last;
-		values[index].value = "";
-
-		/* if we got a value, find the next end marker and
-		 * store the value; otherwise, store empty string */
-		if (c < buffer + size && *c == TELNET_ENVIRON_VALUE) {
-			++c;
-			last = out;
-			while (c < buffer + size) {
-				/* stop when we find the start of the next variable */
-				if ((unsigned)*c == TELNET_ENVIRON_VAR ||
-						(unsigned)*c == TELNET_ENVIRON_USERVAR) {
-					break;
-				}
-
-				/* buffer next byte (taking into account ESC) */
-				if (*c == TELNET_ENVIRON_ESC) {
-					++c;
-				}
-
-				*out++ = *c++;
-			}
-			*out++ = '\0';
-
-			/* store the variable value */
-			values[index].value = last;
-		}
-	}
-
-	/* pass values array and count to event */
-	ev.environ.values = values;
-	ev.environ.size = count;
-
-	/* invoke event with our arguments */
-	ev.type = TELNET_EV_ENVIRON;
-	telnet->eh(telnet, &ev, telnet->ud);
-
-	/* clean up */
-	free(values);
-	return 1;
-}
-
-/* process an MSSP subnegotiation buffer */
-static int _mssp_telnet(telnet_t *telnet, char* buffer, size_t size) {
-	telnet_event_t ev;
-	struct telnet_environ_t *values;
-	char *var = 0;
-	char *c, *last, *out;
-	size_t i, count;
-	unsigned char next_type;
-
-	/* if we have no data, just pass it through */
-	if (size == 0) {
-		return 0;
-	}
-
-	/* first byte must be a VAR */
-	if ((unsigned)buffer[0] != TELNET_MSSP_VAR) {
-		_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-				"MSSP subnegotiation has invalid data");
-		return 0;
-	}
-
-	/* count the arguments, any part that starts with VALUE */
-	for (count = 0, i = 0; i != size; ++i) {
-		if ((unsigned)buffer[i] == TELNET_MSSP_VAL) {
-			++count;
-		}
-	}
-
-	/* allocate argument array, bail on error */
-	if ((values = (struct telnet_environ_t *)calloc(count,
-			sizeof(struct telnet_environ_t))) == 0) {
-		_error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
-				"calloc() failed: %s", strerror(errno));
-		return 0;
-	}
-
-	ev.mssp.values = values;
-	ev.mssp.size = count;
-
-	/* allocate strings in argument array */
-	out = last = buffer;
-	next_type = buffer[0];
-	for (i = 0, c = buffer + 1; c < buffer + size;) {
-		/* search for end marker */
-		while (c < buffer + size && (unsigned)*c != TELNET_MSSP_VAR &&
-				(unsigned)*c != TELNET_MSSP_VAL) {
-			*out++ = *c++;
-		}
-		*out++ = '\0';
-
-		/* if it's a variable name, just store the name for now */
-		if (next_type == TELNET_MSSP_VAR) {
-			var = last;
-		} else if (next_type == TELNET_MSSP_VAL && var != 0) {
-			values[i].var = var;
-			values[i].value = last;
-			++i;
-		} else {
-			_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-					"invalid MSSP subnegotiation data");
-			free(values);
-			return 0;
-		}
-
-		/* remember our next type and increment c for next loop run */
-		last = out;
-		next_type = *c++;
-	}
-
-	/* invoke event with our arguments */
-	ev.type = TELNET_EV_MSSP;
-	telnet->eh(telnet, &ev, telnet->ud);
-
-	/* clean up */
-	free(values);
-
-	return 0;
-}
-
-/* parse ZMP command subnegotiation buffers */
-static int _zmp_telnet(telnet_t *telnet, const char* buffer, size_t size) {
-	telnet_event_t ev;
-	char **argv;
-	const char *c;
-	size_t i, argc;
-
-	/* make sure this is a valid ZMP buffer */
-	if (size == 0 || buffer[size - 1] != 0) {
-		_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-				"incomplete ZMP frame");
-		return 0;
-	}
-
-	/* count arguments */
-	for (argc = 0, c = buffer; c != buffer + size; ++argc)
-		c += strlen(c) + 1;
-
-	/* allocate argument array, bail on error */
-	if ((argv = (char **)calloc(argc, sizeof(char *))) == 0) {
-		_error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
-				"calloc() failed: %s", strerror(errno));
-		return 0;
-	}
-
-	/* populate argument array */
-	for (i = 0, c = buffer; i != argc; ++i) {
-		argv[i] = (char *)c;
-		c += strlen(c) + 1;
-	}
-
-	/* invoke event with our arguments */
-	ev.type = TELNET_EV_ZMP;
-	ev.zmp.argv = (const char**)argv;
-	ev.zmp.argc = argc;
-	telnet->eh(telnet, &ev, telnet->ud);
-
-	/* clean up */
-	free(argv);
-	return 0;
-}
-
-/* parse TERMINAL-TYPE command subnegotiation buffers */
-static int _ttype_telnet(telnet_t *telnet, const char* buffer, size_t size) {
-	telnet_event_t ev;
-
-	/* make sure request is not empty */
-	if (size == 0) {
-		_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-				"incomplete TERMINAL-TYPE request");
-		return 0;
-	}
-
-	/* make sure request has valid command type */
-	if (buffer[0] != TELNET_TTYPE_IS &&
-			buffer[0] != TELNET_TTYPE_SEND) {
-		_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-				"TERMINAL-TYPE request has invalid type");
-		return 0;
-	}
-
-	/* send proper event */
-	if (buffer[0] == TELNET_TTYPE_IS) {
-		char *name;
-
-		/* allocate space for name */
-		if ((name = (char *)malloc(size)) == 0) {
-			_error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
-					"malloc() failed: %s", strerror(errno));
-			return 0;
-		}
-		memcpy(name, buffer + 1, size - 1);
-		name[size - 1] = '\0';
-
-		ev.type = TELNET_EV_TTYPE;
-		ev.ttype.cmd = TELNET_TTYPE_IS;
-		ev.ttype.name = name;
-		telnet->eh(telnet, &ev, telnet->ud);
-
-		/* clean up */
-		free(name);
-	} else {
-		ev.type = TELNET_EV_TTYPE;
-		ev.ttype.cmd = TELNET_TTYPE_SEND;
-		ev.ttype.name = 0;
-		telnet->eh(telnet, &ev, telnet->ud);
-	}
-
-	return 0;
-}
-
-/* process a subnegotiation buffer; return non-zero if the current buffer
- * must be aborted and reprocessed due to COMPRESS2 being activated
- */
-static int _subnegotiate(telnet_t *telnet) {
-	telnet_event_t ev;
-
-	/* standard subnegotiation event */
-	ev.type = TELNET_EV_SUBNEGOTIATION;
-	ev.sub.telopt = telnet->sb_telopt;
-	ev.sub.buffer = telnet->buffer;
-	ev.sub.size = telnet->buffer_pos;
-	telnet->eh(telnet, &ev, telnet->ud);
-
-	switch (telnet->sb_telopt) {
-#if defined(HAVE_ZLIB)
-	/* received COMPRESS2 begin marker, setup our zlib box and
-	 * start handling the compressed stream if it's not already.
-	 */
-	case TELNET_TELOPT_COMPRESS2:
-		if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS2) {
-			if (_init_zlib(telnet, 0, 1) != TELNET_EOK)
-				return 0;
-
-			/* notify app that compression was enabled */
-			ev.type = TELNET_EV_COMPRESS;
-			ev.compress.state = 1;
-			telnet->eh(telnet, &ev, telnet->ud);
-			return 1;
-		}
-		return 0;
-#endif /* defined(HAVE_ZLIB) */
-
-	/* specially handled subnegotiation telopt types */
-	case TELNET_TELOPT_ZMP:
-		return _zmp_telnet(telnet, telnet->buffer, telnet->buffer_pos);
-	case TELNET_TELOPT_TTYPE:
-		return _ttype_telnet(telnet, telnet->buffer, telnet->buffer_pos);
-	case TELNET_TELOPT_ENVIRON:
-	case TELNET_TELOPT_NEW_ENVIRON:
-		return _environ_telnet(telnet, telnet->sb_telopt, telnet->buffer,
-				telnet->buffer_pos);
-	case TELNET_TELOPT_MSSP:
-		return _mssp_telnet(telnet, telnet->buffer, telnet->buffer_pos);
-	default:
-		return 0;
-	}
-}
-
-/* initialize a telnet state tracker */
-telnet_t *telnet_init(const telnet_telopt_t *telopts,
-		telnet_event_handler_t eh, unsigned char flags, void *user_data) {
-	/* allocate structure */
-	struct telnet_t *telnet = (telnet_t*)calloc(1, sizeof(telnet_t));
-	if (telnet == 0)
-		return 0;
-
-	/* initialize data */
-	telnet->ud = user_data;
-	telnet->telopts = telopts;
-	telnet->eh = eh;
-	telnet->flags = flags;
-
-	return telnet;
-}
-
-/* free up any memory allocated by a state tracker */
-void telnet_free(telnet_t *telnet) {
-	/* free sub-request buffer */
-	if (telnet->buffer != 0) {
-		free(telnet->buffer);
-		telnet->buffer = 0;
-		telnet->buffer_size = 0;
-		telnet->buffer_pos = 0;
-	}
-
-#if defined(HAVE_ZLIB)
-	/* free zlib box */
-	if (telnet->z != 0) {
-		if (telnet->flags & TELNET_PFLAG_DEFLATE)
-			deflateEnd(telnet->z);
-		else
-			inflateEnd(telnet->z);
-		free(telnet->z);
-		telnet->z = 0;
-	}
-#endif /* defined(HAVE_ZLIB) */
-
-	/* free RFC1143 queue */
-	if (telnet->q) {
-		free(telnet->q);
-		telnet->q = 0;
-		telnet->q_size = 0;
-	}
-
-	/* free the telnet structure itself */
-	free(telnet);
-}
-
-/* push a byte into the telnet buffer */
-static telnet_error_t _buffer_byte(telnet_t *telnet,
-		unsigned char byte) {
-	char *new_buffer;
-	size_t i;
-
-	/* check if we're out of room */
-	if (telnet->buffer_pos == telnet->buffer_size) {
-		/* find the next buffer size */
-		for (i = 0; i != _buffer_sizes_count; ++i) {
-			if (_buffer_sizes[i] == telnet->buffer_size) {
-				break;
-			}
-		}
-
-		/* overflow -- can't grow any more */
-		if (i >= _buffer_sizes_count - 1) {
-			_error(telnet, __LINE__, __func__, TELNET_EOVERFLOW, 0,
-					"subnegotiation buffer size limit reached");
-			return TELNET_EOVERFLOW;
-		}
-
-		/* (re)allocate buffer */
-		new_buffer = (char *)realloc(telnet->buffer, _buffer_sizes[i + 1]);
-		if (new_buffer == 0) {
-			_error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
-					"realloc() failed");
-			return TELNET_ENOMEM;
-		}
-
-		telnet->buffer = new_buffer;
-		telnet->buffer_size = _buffer_sizes[i + 1];
-	}
-
-	/* push the byte, all set */
-	telnet->buffer[telnet->buffer_pos++] = byte;
-	return TELNET_EOK;
-}
-
-static void _process(telnet_t *telnet, const char *buffer, size_t size) {
-	telnet_event_t ev;
-	unsigned char byte;
-	size_t i, start;
-	for (i = start = 0; i != size; ++i) {
-		byte = buffer[i];
-		switch (telnet->state) {
-		/* regular data */
-		case TELNET_STATE_DATA:
-			/* on an IAC byte, pass through all pending bytes and
-			 * switch states */
-			if (byte == TELNET_IAC) {
-				if (i != start) {
-					ev.type = TELNET_EV_DATA;
-					ev.data.buffer = buffer + start;
-					ev.data.size = i - start;
-					telnet->eh(telnet, &ev, telnet->ud);
-				}
-				telnet->state = TELNET_STATE_IAC;
-			}
-			break;
-
-		/* IAC command */
-		case TELNET_STATE_IAC:
-			switch (byte) {
-			/* subnegotiation */
-			case TELNET_SB:
-				telnet->state = TELNET_STATE_SB;
-				break;
-			/* negotiation commands */
-			case TELNET_WILL:
-				telnet->state = TELNET_STATE_WILL;
-				break;
-			case TELNET_WONT:
-				telnet->state = TELNET_STATE_WONT;
-				break;
-			case TELNET_DO:
-				telnet->state = TELNET_STATE_DO;
-				break;
-			case TELNET_DONT:
-				telnet->state = TELNET_STATE_DONT;
-				break;
-			/* IAC escaping */
-			case TELNET_IAC:
-				/* event */
-				ev.type = TELNET_EV_DATA;
-				ev.data.buffer = (char*)&byte;
-				ev.data.size = 1;
-				telnet->eh(telnet, &ev, telnet->ud);
-
-				/* state update */
-				start = i + 1;
-				telnet->state = TELNET_STATE_DATA;
-				break;
-			/* some other command */
-			default:
-				/* event */
-				ev.type = TELNET_EV_IAC;
-				ev.iac.cmd = byte;
-				telnet->eh(telnet, &ev, telnet->ud);
-
-				/* state update */
-				start = i + 1;
-				telnet->state = TELNET_STATE_DATA;
-			}
-			break;
-
-		/* negotiation commands */
-		case TELNET_STATE_WILL:
-		case TELNET_STATE_WONT:
-		case TELNET_STATE_DO:
-		case TELNET_STATE_DONT:
-			_negotiate(telnet, byte);
-			start = i + 1;
-			telnet->state = TELNET_STATE_DATA;
-			break;
-
-		/* subnegotiation -- determine subnegotiation telopt */
-		case TELNET_STATE_SB:
-			telnet->sb_telopt = byte;
-			telnet->buffer_pos = 0;
-			telnet->state = TELNET_STATE_SB_DATA;
-			break;
-
-		/* subnegotiation -- buffer bytes until end request */
-		case TELNET_STATE_SB_DATA:
-			/* IAC command in subnegotiation -- either IAC SE or IAC IAC */
-			if (byte == TELNET_IAC) {
-				telnet->state = TELNET_STATE_SB_DATA_IAC;
-			} else if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS && byte == TELNET_WILL) {
-				/* In 1998 MCCP used TELOPT 85 and the protocol defined an invalid
-				 * subnegotiation sequence (IAC SB 85 WILL SE) to start compression.
-				 * Subsequently MCCP version 2 was created in 2000 using TELOPT 86
-				 * and a valid subnegotiation (IAC SB 86 IAC SE). libtelnet for now
-				 * just captures and discards MCCPv1 sequences.
-				 */
-				start = i + 2;
-				telnet->state = TELNET_STATE_DATA;
-			/* buffer the byte, or bail if we can't */
-			} else if (_buffer_byte(telnet, byte) != TELNET_EOK) {
-				start = i + 1;
-				telnet->state = TELNET_STATE_DATA;
-			}
-			break;
-
-		/* IAC escaping inside a subnegotiation */
-		case TELNET_STATE_SB_DATA_IAC:
-			switch (byte) {
-			/* end subnegotiation */
-			case TELNET_SE:
-				/* return to default state */
-				start = i + 1;
-				telnet->state = TELNET_STATE_DATA;
-
-				/* process subnegotiation */
-				if (_subnegotiate(telnet) != 0) {
-					/* any remaining bytes in the buffer are compressed.
-					 * we have to re-invoke telnet_recv to get those
-					 * bytes inflated and abort trying to process the
-					 * remaining compressed bytes in the current _process
-					 * buffer argument
-					 */
-					telnet_recv(telnet, &buffer[start], size - start);
-					return;
-				}
-				break;
-			/* escaped IAC byte */
-			case TELNET_IAC:
-				/* push IAC into buffer */
-				if (_buffer_byte(telnet, TELNET_IAC) !=
-						TELNET_EOK) {
-					start = i + 1;
-					telnet->state = TELNET_STATE_DATA;
-				} else {
-					telnet->state = TELNET_STATE_SB_DATA;
-				}
-				break;
-			/* something else -- protocol error.  attempt to process
-			 * content in subnegotiation buffer, then evaluate the
-			 * given command as an IAC code.
-			 */
-			default:
-				_error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
-						"unexpected byte after IAC inside SB: %d",
-						byte);
-
-				/* enter IAC state */
-				start = i + 1;
-				telnet->state = TELNET_STATE_IAC;
-
-				/* process subnegotiation; see comment in
-				 * TELNET_STATE_SB_DATA_IAC about invoking telnet_recv()
-				 */
-				if (_subnegotiate(telnet) != 0) {
-					telnet_recv(telnet, &buffer[start], size - start);
-					return;
-				} else {
-					/* recursive call to get the current input byte processed
-					 * as a regular IAC command.  we could use a goto, but
-					 * that would be gross.
-					 */
-					_process(telnet, (char *)&byte, 1);
-				}
-				break;
-			}
-			break;
-		}
-	}
-
-	/* pass through any remaining bytes */ 
-	if (telnet->state == TELNET_STATE_DATA && i != start) {
-		ev.type = TELNET_EV_DATA;
-		ev.data.buffer = buffer + start;
-		ev.data.size = i - start;
-		telnet->eh(telnet, &ev, telnet->ud);
-	}
-}
-
-/* push a bytes into the state tracker */
-void telnet_recv(telnet_t *telnet, const char *buffer,
-		size_t size) {
-#if defined(HAVE_ZLIB)
-	/* if we have an inflate (decompression) zlib stream, use it */
-	if (telnet->z != 0 && !(telnet->flags & TELNET_PFLAG_DEFLATE)) {
-		char inflate_buffer[1024];
-		int rs;
-
-		/* initialize zlib state */
-		telnet->z->next_in = (unsigned char*)buffer;
-		telnet->z->avail_in = (unsigned int)size;
-		telnet->z->next_out = (unsigned char *)inflate_buffer;
-		telnet->z->avail_out = sizeof(inflate_buffer);
-
-		/* inflate until buffer exhausted and all output is produced */
-		while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
-			/* reset output buffer */
-
-			/* decompress */
-			rs = inflate(telnet->z, Z_SYNC_FLUSH);
-
-			/* process the decompressed bytes on success */
-			if (rs == Z_OK || rs == Z_STREAM_END)
-				_process(telnet, inflate_buffer, sizeof(inflate_buffer) -
-						telnet->z->avail_out);
-			else
-				_error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
-						"inflate() failed: %s", zError(rs));
-
-			/* prepare output buffer for next run */
-			telnet->z->next_out = (unsigned char *)inflate_buffer;
-			telnet->z->avail_out = sizeof(inflate_buffer);
-
-			/* on error (or on end of stream) disable further inflation */
-			if (rs != Z_OK) {
-				telnet_event_t ev;
-
-				/* disable compression */
-				inflateEnd(telnet->z);
-				free(telnet->z);
-				telnet->z = 0;
-
-				/* send event */
-				ev.type = TELNET_EV_COMPRESS;
-				ev.compress.state = 0;
-				telnet->eh(telnet, &ev, telnet->ud);
-
-				break;
-			}
-		}
-
-	/* COMPRESS2 is not negotiated, just process */
-	} else
-#endif /* defined(HAVE_ZLIB) */
-		_process(telnet, buffer, size);
-}
-
-/* send an iac command */
-void telnet_iac(telnet_t *telnet, unsigned char cmd) {
-	unsigned char bytes[2];
-	bytes[0] = TELNET_IAC;
-	bytes[1] = cmd;
-	_sendu(telnet, bytes, 2);
-}
-
-/* send negotiation */
-void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
-		unsigned char telopt) {
-	telnet_rfc1143_t q;
-
-	/* if we're in proxy mode, just send it now */
-	if (telnet->flags & TELNET_FLAG_PROXY) {
-		unsigned char bytes[3];
-		bytes[0] = TELNET_IAC;
-		bytes[1] = cmd;
-		bytes[2] = telopt;
-		_sendu(telnet, bytes, 3);
-		return;
-	}
-	
-	/* get current option states */
-	q = _get_rfc1143(telnet, telopt);
-
-	switch (cmd) {
-	/* advertise willingess to support an option */
-	case TELNET_WILL:
-		switch (Q_US(q)) {
-		case Q_NO:
-			_set_rfc1143(telnet, telopt, Q_WANTYES, Q_HIM(q));
-			_send_negotiate(telnet, TELNET_WILL, telopt);
-			break;
-		case Q_WANTNO:
-			_set_rfc1143(telnet, telopt, Q_WANTNO_OP, Q_HIM(q));
-			break;
-		case Q_WANTYES_OP:
-			_set_rfc1143(telnet, telopt, Q_WANTYES, Q_HIM(q));
-			break;
-		}
-		break;
-
-	/* force turn-off of locally enabled option */
-	case TELNET_WONT:
-		switch (Q_US(q)) {
-		case Q_YES:
-			_set_rfc1143(telnet, telopt, Q_WANTNO, Q_HIM(q));
-			_send_negotiate(telnet, TELNET_WONT, telopt);
-			break;
-		case Q_WANTYES:
-			_set_rfc1143(telnet, telopt, Q_WANTYES_OP, Q_HIM(q));
-			break;
-		case Q_WANTNO_OP:
-			_set_rfc1143(telnet, telopt, Q_WANTNO, Q_HIM(q));
-			break;
-		}
-		break;
-
-	/* ask remote end to enable an option */
-	case TELNET_DO:
-		switch (Q_HIM(q)) {
-		case Q_NO:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES);
-			_send_negotiate(telnet, TELNET_DO, telopt);
-			break;
-		case Q_WANTNO:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO_OP);
-			break;
-		case Q_WANTYES_OP:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES);
-			break;
-		}
-		break;
-
-	/* demand remote end disable an option */
-	case TELNET_DONT:
-		switch (Q_HIM(q)) {
-		case Q_YES:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO);
-			_send_negotiate(telnet, TELNET_DONT, telopt);
-			break;
-		case Q_WANTYES:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES_OP);
-			break;
-		case Q_WANTNO_OP:
-			_set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO);
-			break;
-		}
-		break;
-	}
-}
-
-/* send non-command data (escapes IAC bytes) */
-void telnet_send(telnet_t *telnet, const char *buffer,
-		size_t size) {
-	size_t i, l;
-
-	for (l = i = 0; i != size; ++i) {
-		/* dump prior portion of text, send escaped bytes */
-		if (buffer[i] == (char)TELNET_IAC) {
-			/* dump prior text if any */
-			if (i != l) {
-				_send(telnet, buffer + l, i - l);
-			}
-			l = i + 1;
-
-			/* send escape */
-			telnet_iac(telnet, TELNET_IAC);
-		}
-	}
-
-	/* send whatever portion of buffer is left */
-	if (i != l) {
-		_send(telnet, buffer + l, i - l);
-	}
-}
-
-/* send subnegotiation header */
-void telnet_begin_sb(telnet_t *telnet, unsigned char telopt) {
-	unsigned char sb[3];
-	sb[0] = TELNET_IAC;
-	sb[1] = TELNET_SB;
-	sb[2] = telopt;
-	_sendu(telnet, sb, 3);
-}
-
-
-/* send complete subnegotiation */
-void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
-		const char *buffer, size_t size) {
-	unsigned char bytes[5];
-	bytes[0] = TELNET_IAC;
-	bytes[1] = TELNET_SB;
-	bytes[2] = telopt;
-	bytes[3] = TELNET_IAC;
-	bytes[4] = TELNET_SE;
-
-	_sendu(telnet, bytes, 3);
-	telnet_send(telnet, buffer, size);
-	_sendu(telnet, bytes + 3, 2);
-
-#if defined(HAVE_ZLIB)
-	/* if we're a proxy and we just sent the COMPRESS2 marker, we must
-	 * make sure all further data is compressed if not already.
-	 */
-	if (telnet->flags & TELNET_FLAG_PROXY &&
-			telopt == TELNET_TELOPT_COMPRESS2) {
-		telnet_event_t ev;
-
-		if (_init_zlib(telnet, 1, 1) != TELNET_EOK)
-			return;
-
-		/* notify app that compression was enabled */
-		ev.type = TELNET_EV_COMPRESS;
-		ev.compress.state = 1;
-		telnet->eh(telnet, &ev, telnet->ud);
-	}
-#endif /* defined(HAVE_ZLIB) */
-}
-
-void telnet_begin_compress2(telnet_t *telnet) {
-#if defined(HAVE_ZLIB)
-	static const unsigned char compress2[] = { TELNET_IAC, TELNET_SB,
-			TELNET_TELOPT_COMPRESS2, TELNET_IAC, TELNET_SE };
-
-	telnet_event_t ev;
-
-	/* attempt to create output stream first, bail if we can't */
-	if (_init_zlib(telnet, 1, 0) != TELNET_EOK)
-		return;
-
-	/* send compression marker.  we send directly to the event handler
-	 * instead of passing through _send because _send would result in
-	 * the compress marker itself being compressed.
-	 */
-	ev.type = TELNET_EV_SEND;
-	ev.data.buffer = (const char*)compress2;
-	ev.data.size = sizeof(compress2);
-	telnet->eh(telnet, &ev, telnet->ud);
-
-	/* notify app that compression was successfully enabled */
-	ev.type = TELNET_EV_COMPRESS;
-	ev.compress.state = 1;
-	telnet->eh(telnet, &ev, telnet->ud);
-#endif /* defined(HAVE_ZLIB) */
-}
-
-/* send formatted data with \r and \n translation in addition to IAC IAC */
-int telnet_vprintf(telnet_t *telnet, const char *fmt, va_list va) {
-    static const char CRLF[] = { '\r', '\n' };
-    static const char CRNUL[] = { '\r', '\0' };
-	char buffer[1024];
-	char *output = buffer;
-	int rs, i, l;
-
-	/* format */
-	rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
-	if (rs >= sizeof(buffer)) {
-		output = (char*)malloc(rs + 1);
-		if (output == 0) {
-			_error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
-					"malloc() failed: %s", strerror(errno));
-			return -1;
-		}
-		rs = vsnprintf(output, rs + 1, fmt, va);
-	}
-
-	/* send */
-	for (l = i = 0; i != rs; ++i) {
-		/* special characters */
-		if (output[i] == (char)TELNET_IAC || output[i] == '\r' ||
-				output[i] == '\n') {
-			/* dump prior portion of text */
-			if (i != l)
-				_send(telnet, output + l, i - l);
-			l = i + 1;
-
-			/* IAC -> IAC IAC */
-			if (output[i] == (char)TELNET_IAC)
-				telnet_iac(telnet, TELNET_IAC);
-			/* automatic translation of \r -> CRNUL */
-			else if (output[i] == '\r')
-				_send(telnet, CRNUL, 2);
-			/* automatic translation of \n -> CRLF */
-			else if (output[i] == '\n')
-				_send(telnet, CRLF, 2);
-		}
-	}
-
-	/* send whatever portion of output is left */
-	if (i != l) {
-		_send(telnet, output + l, i - l);
-	}
-
-	/* free allocated memory, if any */
-	if (output != buffer) {
-		free(output);
-	}
-
-	return rs;
-}
-
-/* see telnet_vprintf */
-int telnet_printf(telnet_t *telnet, const char *fmt, ...) {
-	va_list va;
-	int rs;
-
-	va_start(va, fmt);
-	rs = telnet_vprintf(telnet, fmt, va);
-	va_end(va);
-
-	return rs;
-}
-
-/* send formatted data through telnet_send */
-int telnet_raw_vprintf(telnet_t *telnet, const char *fmt, va_list va) {
-	char buffer[1024];
-	char *output = buffer;
-	int rs;
-
-	/* format; allocate more space if necessary */
-	rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
-	if (rs >= sizeof(buffer)) {
-		output = (char*)malloc(rs + 1);
-		if (output == 0) {
-			_error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
-					"malloc() failed: %s", strerror(errno));
-			return -1;
-		}
-		rs = vsnprintf(output, rs + 1, fmt, va);
-	}
-
-	/* send out the formatted data */
-	telnet_send(telnet, output, rs);
-
-	/* release allocated memory, if any */
-	if (output != buffer) {
-		free(output);
-	}
-
-	return rs;
-}
-
-/* see telnet_raw_vprintf */
-int telnet_raw_printf(telnet_t *telnet, const char *fmt, ...) {
-	va_list va;
-	int rs;
-
-	va_start(va, fmt);
-	rs = telnet_raw_vprintf(telnet, fmt, va);
-	va_end(va);
-
-	return rs;
-}
-
-/* begin NEW-ENVIRON subnegotation */
-void telnet_begin_newenviron(telnet_t *telnet, unsigned char cmd) {
-	telnet_begin_sb(telnet, TELNET_TELOPT_NEW_ENVIRON);
-	telnet_send(telnet, (const char *)&cmd, 1);
-}
-
-/* send a NEW-ENVIRON value */
-void telnet_newenviron_value(telnet_t *telnet, unsigned char type,
-		const char *string) {
-	telnet_send(telnet, (const char*)&type, 1);
-
-	if (string != 0) {
-		telnet_send(telnet, string, strlen(string));
-	}
-}
-
-/* send TERMINAL-TYPE SEND command */
-void telnet_ttype_send(telnet_t *telnet) {
-    static const unsigned char SEND[] = { TELNET_IAC, TELNET_SB,
-			TELNET_TELOPT_TTYPE, TELNET_TTYPE_SEND, TELNET_IAC, TELNET_SE };
-	_sendu(telnet, SEND, sizeof(SEND));
-}
-
-/* send TERMINAL-TYPE IS command */
-void telnet_ttype_is(telnet_t *telnet, const char* ttype) {
-	static const unsigned char IS[] = { TELNET_IAC, TELNET_SB,
-			TELNET_TELOPT_TTYPE, TELNET_TTYPE_IS };
-	_sendu(telnet, IS, sizeof(IS));
-	_send(telnet, ttype, strlen(ttype));
-	telnet_finish_sb(telnet);
-}
-
-/* send ZMP data */
-void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv) {
-	size_t i;
-
-	/* ZMP header */
-	telnet_begin_zmp(telnet, argv[0]);
-
-	/* send out each argument, including trailing NUL byte */
-	for (i = 1; i != argc; ++i)
-		telnet_zmp_arg(telnet, argv[i]);
-
-	/* ZMP footer */
-	telnet_finish_zmp(telnet);
-}
-
-/* send ZMP data using varargs  */
-void telnet_send_vzmpv(telnet_t *telnet, va_list va) {
-	const char* arg;
-
-	/* ZMP header */
-	telnet_begin_sb(telnet, TELNET_TELOPT_ZMP);
-
-	/* send out each argument, including trailing NUL byte */
-	while ((arg = va_arg(va, const char *)) != 0)
-		telnet_zmp_arg(telnet, arg);
-
-	/* ZMP footer */
-	telnet_finish_zmp(telnet);
-}
-
-/* see telnet_send_vzmpv */
-void telnet_send_zmpv(telnet_t *telnet, ...) {
-	va_list va;
-
-	va_start(va, telnet);
-	telnet_send_vzmpv(telnet, va);
-	va_end(va);
-}
-
-/* begin a ZMP command */
-void telnet_begin_zmp(telnet_t *telnet, const char *cmd) {
-	telnet_begin_sb(telnet, TELNET_TELOPT_ZMP);
-	telnet_zmp_arg(telnet, cmd);
-}
-
-/* send a ZMP argument */
-void telnet_zmp_arg(telnet_t *telnet, const char* arg) {
-	telnet_send(telnet, arg, strlen(arg) + 1);
-}

+ 0 - 122
oldsrc/main.c

@@ -1,122 +0,0 @@
-#define _POSIX_C_SOURCE 200801L
-#define _POSIX_SOURCE 200801L
-
-#include <stdlib.h>
-#include <unistd.h>
-#include <signal.h>
-#include <errno.h>
-#include <time.h>
-
-
-#include "server.h"
-#include "monolog.h"
-#include "config.h"
-#include "state.h"
-#include "rh.h"
-#include "toruby.h"
-#include "timer.h"
-
-
-
-DEFINE_FILE_LOGGER(woe_file_logger);
-DEFINE_STDOUT_LOGGER(woe_stdout_logger);
-DEFINE_STDERR_LOGGER(woe_stderr_logger);
-
-
-FILE * woe_start_monolog(char * logname) {
-  FILE * fout;
-
-  monolog_init();
-  LOG_ENABLE_ERROR();
-  monolog_enable_level("WARNING");
-  monolog_enable_level("NOTE");
-
-  fout = fopen(logname, "w");
-  
-  monolog_add_logger(NULL, &woe_stdout_logger);
-  
-  if (fout) {
-    monolog_add_logger(fout, &woe_file_logger);
-    fprintf(stderr, "Opened log file %s %p\n", logname, fout);
-  } else {
-    fprintf(stderr, "Could not open log file %s\n", logname);
-  }
-    
-  return fout;
-}
-
-
-
-void woe_stop_monolog() {  
-  monolog_done();
-}
-
-
-int main(int argc, char * argv[]) {
-  sigset_t            signal_set  = { { 0 } };
-  sigset_t            signal_old  = { { 0 } };
-  siginfo_t           signal_info = { 0 };
-  struct timespec     signal_time = { 0, 10 };
-  
-  struct woe_state    state  = { 0 };
-  struct woe_config   config = { 0 };  
-  state.config     = &config;
-    
-  FILE * logfile   = NULL;
-  state.mrb        = mrb_open();
-  state.mrb->ud    = &state;
-  tr_init(state.mrb);
-  woe_config_init_args(state.config, argc, argv);
-  
-  logfile          = woe_start_monolog(state.config->log_file);
-  
-  state.server     = woe_server_new(state.config->port);
-  
-  woe_server_set_mrb(state.server, state.mrb);
-  
-  /* Handle all signals, except a few. */
-  sigfillset(&signal_set);
-  sigdelset(&signal_set, SIGKILL);
-  sigdelset(&signal_set, SIGTERM);
-  sigdelset(&signal_set, SIGINT);
-  sigdelset(&signal_set, SIGSEGV);
-  sigdelset(&signal_set, SIGTSTP);
-  
-  /* Ignore them all, because we will wait for them in stead. */
-  sigprocmask(SIG_SETMASK, &signal_set, &signal_old);
-
-  
-  rh_run_script(state.mrb, "main.rb");
-  
-  if (woe_server_listen(state.server) > 0) {
-     LOG_ERROR("Cannot listen. Stop.");   
-  } else {
-    rh_run_toplevel(state.mrb, "woe_on_start", "");
-    while (woe_server_busy(state.server)) {
-      int caught = 0;
-      siginfo_t info;
-      struct woe_timer * timer;
-      info.si_value.sival_ptr = NULL;
-      woe_server_update(state.server, 1);
-      caught = sigtimedwait(&signal_set, &info, &signal_time);
-      if (caught > 0) {
-        timer = info.si_value.sival_ptr; 
-        if (timer) { 
-          LOG_DEBUG("Received timer signal %d, %d\n", caught, timer->index);
-          woe_timer_callback(timer);
-        } else {
-          LOG_NOTE("Received signal %d\n", caught);
-          rh_run_toplevel(state.mrb, "woe_on_signal", "i", caught);
-        }
-      }
-    }
-  }
- 
-  rh_run_toplevel(state.mrb, "woe_on_stop", "");
-  woe_server_free(state.server);
-  mrb_close(state.mrb);
-  LOG_NOTE("Shutting down WOE\n");
-  
-  woe_stop_monolog();
-  return 0;
-}

+ 0 - 257
oldsrc/mem.c

@@ -1,257 +0,0 @@
-/* Memory handling, malloc wrapers, etc */
-
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-
-#include "mem.h"
-
-
-/* Calls exit(EXIT_FAILURE) if ptr is NULL. Is message is not null, prints this on stderr.  */
-void * exit_if_null(void * ptr, char * message) {
-  if(!ptr) {
-    if(message) {
-      fprintf(stderr, "%s\n", message);
-    }
-    exit(EXIT_FAILURE);
-  }
-  return ptr;
-}
-
-#define EXIT_ON_NOMEMORY(PTR) exit_if_null(PTR, "Out of memory!")
-
-/** Wrapper for calloc/malloc */
-void * mem_calloc(size_t amount, size_t size) {
-  void * ptr = calloc(amount, size);
-  return EXIT_ON_NOMEMORY(ptr);
-}
-
-/** Wrapper for calloc/malloc */
-void * mem_alloc(size_t size) {
-  void * ptr = mem_calloc(1, size);
-  return EXIT_ON_NOMEMORY(ptr);
-}
-
-/** Wrapper for free */
-void * mem_free(void * ptr) {
-  free(ptr);
-  return NULL;
-}
-
-/** Wrapper for realloc */
-void * mem_realloc(void *ptr, size_t newsize) {
-  void * aid = realloc(ptr, newsize);
-  return EXIT_ON_NOMEMORY(aid);
-}
-
-/** Resizes memory, taking care not to lose ptr* if the reallocation
-fails. */
-void * mem_resize(void ** ptr, size_t newsize) {
-  if(*ptr) { 
-    void * res = mem_realloc((*ptr), newsize);
-    if(res) {
-      (*ptr) = res;
-      return res;
-    }
-  }
-  return NULL;
-}
-
-/** Wrapper for memmove, for consistency */
-void * mem_move(void * dest, void * src, size_t size) {
-  return memmove(dest , src, size);
-}
-
-/* Thoughts on hierarchical data structures.
-* Often, in C data is allocatedin a hierarchical way. 
-* It's useful to have functions that take this into consideration...
-*/ 
-
-typedef struct Memtree_ Memtree;
-
-struct Memtree_ {
-  void * memory;
-  size_t size;
-  Memtree * next, * parent, * child;
-};
-
-#ifdef COMMENT_
-/* Tree memory allocator. */
-void * mem_tree_alloc(void * vparent, size_t size) {
-  char * aid;
-  Memtree * me, * next;
-  Memtree * parent = vparent;
-  aid        = mem_calloc(1, size + sizeof(Memtree));
-  if (!aid) return NULL;
-  me         = (Memtree *) aid;
-  me->memory = aid + sizeof(Memtree); 
-  me->size   = size;
-  if (parent) {
-    me->parent = parent;
-    if (parent->child) {
-      next = parent->child;
-    } else {
-      next = NULL
-    }
-        
-    parent->child = me;
-    me->next      = next;
-  } else {
-    me->parent   = NULL;
-    me->next     = NULL;
-    me->child    = NULL;
-  }
-  return me->memory;
-}
-
-
-void mem_tree_free(void * vparent) {
-  char * aid;
-  Memtree * me, * next;
-  Memtree * parent = vparent;
-  aid        = vparent;
-  if (!aid) return;
-  me         = (Memtree *) (aid + sizeof(Memtree));
-  if (!me) return;
-  me->memory = NULL;
-  me->size   = 0; 
-  if (me->child) {
-    mem_tree_free(me->child);
-  } else if (me->next) {
-    for (next = me->next; next; next = next->next) {
-      
-      mem_tree_free(next);
-    } 
-  } else {
-    next = NULL
-  }
-  
-  mem_free(me);  
-}
-
-#endif
-
-/* 
-* Thoughts on memory allocation in C. 
-*
-* In theory, memory allocations could just use malloc an free and 
-* that would be enough, however, in practise there are several factors that
-* mean that just calling free() on any pointer won't free all allocated
-* memory and resources associated to it. In general, when a pointer to 
-* a resource needs to be cleaned up, there are 3 concens to take care of:
-*
-* 1) Cleaning up of non-memory resources like OS handles, API haandles, 
-* FILEs, etc. that must be closed or cleaned up before the pointer/struct 
-* that refers to them may be deallocated. In general, this could be 
-* called deinitialization.
-*
-* 2) The second problem is that the pointer may point to a complex data
-* structure that may have been allocated my several mallocs. In this case
-* multiple frees are needed. This could be called deconstruction.
-* 
-* 3) Finally the pointer will usually point to allocated memory that must 
-* be deallocated. For this, free could suffice, but not in all cases.
-* This could be deallocation.
-*
-* These 3 steps are of course mirrored when the resource is created, as can 
-* be seen in the diagram below.
-* 
-*               step                  state of resource
-*
-* Before creation:                  : VOID
-*  
-*           /- allocation           : ALLOCATED
-* Creation {-- construction         : CONSTRUCTED
-*           \- initialization       : INITIALIZED
-*
-* Use of the resource....
-*
-*              /- deinitialization  : CONSTRUCTED
-* Destruction {-- deconstruction    : ALLLOCATED
-*              \- deallocation      : VOID
-*
-* It should be noticed that with proper care an object that has not been
-* intialized, or that merely has been deinitialized (i.e. in the CONSTRUCTED
-* state) could be used or reused simply by initializing or reinitializing it. 
-* It could be useful for performance or practical reasons or to enable such
-* recycling of already allocated and constructed resources. 
-* 
-* On the other hand a resource that has been deallocated or not allocated 
-* cannnot be used correctly at all. What then about a resource that has not 
-* been constructed yet or one that has already been deconstructed? The barrier
-* is more fluid and uncrear here. However, in many progaming languages, these
-* 3 steps get simplified ito 2 or even 1 step. However, experience shows that 
-* having only one step is too gross, especially in the face of reference 
-* counting or on-GC deinitialization. Therefore, it's best to have 2
-* steps on construction and 2 steps on deconstruction namely 
-*
-* construction by XXX_new  : XXX_build    XXX_init. 
-* destruction  by XXX_free : XXX_done     XXX_demolish
-*
-* The rule is that XXX_done and XXX_init should be complementary, 
-* any resource RES that has XXX_init(XXX_done(RES), params...) called 
-* will be usable again. Furthemore XXX_done should be idempotent,
-* that is XXX_done(XXX_done(RES)), etc is allowed. 
-* XXX_init need not be idempotent as long as it can undo XXX_done correctly.
-* XXX_free is guaranteed to call XXX_done and XXX_demolish. 
-* XXX_demolish must not neccesarily be exposed on the public API or 
-* even exist.
-* XXX_new calls XXX_build and XXX_init in turn.
-*
-*
-* The problem remains then : how to know which destructor to call?
-* In case it's known at compile-time  which other destructors will be needed
-* it's simple. However, some containers may want to store objects whose type 
-* is only known at run time. For them it is neccesary to either not accept 
-* ownership and let some other routine deallocate them) which is 
-* bothersome, certainy for, say, hash tables, or to properly devise a system
-* where the correct destructors can be passed at run time. 
-* 
-* And then we arrive at interfaces that use and/or consist of virtual tables, 
-* that is tablew with function pointers. For the destructor a single pointer
-* could suffice, but while we're going virtual, allowing interface/class like
-* tables will be better since it alllows more generality and saves space.
-*
-*
-* Another point is what to do if memory allocation fails. While for a critical 
-* application, it woule make sense to try to free ups some space and try
-* again, for non-critical ones like Eruta, the best thing to do is to 
-* simply end the program, since there's nothing sensible that could be done,
-* apart from trying to restore the sceen to it's previous resolution if possible.
-*  
-*
-*/
-
-
-
-/** Thoughts on memory management and garbage collection. 
-* The probelm in C is that it is not always clear who is the owner 
-* for an object, and and who is just using a reference. 
-* If ownership is well defined, then correct cleanup is easier to arrange.
-*
-* Here's an idea on reference linking for shared ownership:
-*/
-
-typedef struct Memref_ Memref;
-
-struct Memref_ {
-  void   * pointer;
-  Memref * next;
-  Memref * previous;
-};
-
-Memref memref(void * pointer) {
-  Memref ref = { pointer, 0, 0 };
-  return ref;
-}
-
-
-
-
-
-
-
-
-
-
-

+ 0 - 306
oldsrc/monolog.c

@@ -1,306 +0,0 @@
-
-#include "monolog.h"
-#include "dynar.h"
-
-#include <string.h>
-
-struct Monologger {
-  struct MonologLoggerInterface logger;
-  void                        * data;
-  int                           index;
-};
-
-struct Monolevel {
-  char * name;
-  int enabled;
-};
-
-
-struct Monolog {
-  Dynar * loggers;
-  Dynar * levels;
-  int last_logger;
-};
-
-static struct Monolog monolog_struct = { NULL, NULL, 0 };
-static struct Monolog *      monolog = &monolog_struct;
-
-void monolog_done() {
-  int index;
-  if (monolog->loggers) {
-    /* clean up all loggers */
-    for (index = 0; index < dynar_size(monolog->loggers); index ++) {
-      monolog_remove_logger(index);
-    }
-     
-    dynar_free(monolog->loggers);
-    monolog->loggers = NULL;
-  }
-  
-  if (monolog->levels) {
-    dynar_free(monolog->levels);
-    monolog->levels = NULL;
-  }
-
-  monolog->last_logger = 0;
-}
-
-/* Compares loggers with each other for sorting.  This is qsort compatible. */
-static int monolog_compare_loggers(void * p1, void * p2) {
-  struct Monologger * me   = *((struct Monologger **) p1);
-  struct Monologger * you  = *((struct Monologger **) p2);
-  if (!me) return -1;
-  if (!you) return 1;
-  if (!me->logger.log) return -1;
-  if (!you->logger.log) return 1;
-
-  /* Compare index if no nulls. */
-  if (me->index < you->index) return -1;
-  if (me->index > you->index) return  1;
-  return 0;
-}
-
-
-/* Compares loglevels with each other for sorting.  This is qsort compatible. */
-static int monolog_compare_loglevels(const void * p1, const void * p2) {
-  const struct Monolevel * me   = p1;
-  const struct Monolevel * you  = p2;
-  if(!me) return -1;
-  if(!you) return 1;
-  if(!me->name) return -1;
-  if(!you->name) return 1;
-  /* Compare name if no nulls. */
-  return strcmp(me->name, you->name);
-  return 0;
-}
-
-
-
-
-#define MONOLOG_START_LEVELS  32
-#define MONOLOG_START_LOGGERS 32
-
-static int monolog_get_loggers_max() {
-  return dynar_size(monolog->loggers);
-}
-
-static struct Monologger * monolog_get_logger(int index) {
-  return dynar_getdata(monolog->loggers, index);
-}
-
-static int monolog_get_unused_logger() {
-  int index;
-  for (index = 0 ; index < dynar_size(monolog->loggers); index++) {
-    struct Monologger * logger = monolog_get_logger(index);
-    if (!logger->logger.log) {
-      return index;
-    }
-  }
-  return -1;
-}
-
-
-static struct Monologger *
-monolog_set_logger(int index, void *data, struct MonologLoggerInterface * ifa) {
-  struct Monologger * logger = monolog_get_logger(index);
-  if(!logger) return NULL;
-  logger->index  = index;
-  logger->data   = data;
-  logger->logger = (*ifa);
-  return logger;
-}
-
-static struct Monolevel * monolog_get_level(int index) {
-  return dynar_getdata(monolog->levels, index);
-}
-
-static int monolog_get_unused_level() {
-  int index;
-  for (index = 0 ; index < dynar_size(monolog->levels); index++) {
-    struct Monolevel * level = monolog_get_level(index);
-    if (!level->name) {
-      return index;
-    }
-  }
-  return -1;
-}
-
-
-static struct Monolevel *
-monolog_set_level (int index, char * name, int enabled) {
-  struct Monolevel * level = monolog_get_level(index);
-  if (!level) return NULL;
-  level->name     = name;
-  level->enabled  = enabled;
-  return level;
-}
-
-
-int monolog_init() {
-  int index;
-  monolog->loggers = dynar_new(MONOLOG_START_LEVELS, sizeof(struct Monologger));
-  if(!monolog->loggers) {
-    return FALSE;
-  }
-  
-  monolog->levels  = dynar_new(MONOLOG_START_LOGGERS, sizeof(struct Monolevel));
-    if(!monolog->levels) {
-    monolog_done();
-    return FALSE;
-  }
-
-  for (index = 0; index < dynar_amount(monolog->loggers) ; index++ ) {
-    struct MonologLoggerInterface ifa = { NULL, NULL };
-    monolog_set_logger(index, NULL, &ifa);
-  }
-  
-  for (index = 0; index < dynar_amount(monolog->levels) ; index++ ) {
-    monolog_set_level(index, NULL, FALSE);
-  }
-  
-  return TRUE;
-}
-
-
-
-/** Adds a loger. Returns negative or error zero or positive (the index of the logger)
- * on success. */
-int monolog_add_logger(void * data, struct MonologLoggerInterface * logger) {
-  int logindex;
-
-  if (monolog->last_logger >= dynar_amount(monolog->loggers))   {
-    if (!dynar_grow(monolog->loggers, MONOLOG_START_LOGGERS)) {
-      return -1;
-    }
-  }
-
-  logindex = monolog_get_unused_logger();
-  if (logindex < 0) {
-    return -2;
-  }
-
-  monolog_set_logger(logindex, data, logger);
-  return logindex;
-}
-
-
-int monolog_remove_logger(int index) {
-  struct MonologLoggerInterface ifa = { NULL, NULL };
-  struct Monologger * logger = monolog_get_logger(index);
-  /* Call logger's destructor if needed. */
-  if (logger->logger.free) {
-    logger->logger.free(logger->data);
-  }
-  monolog_set_logger(index, NULL, &ifa);
-  return index;
-} 
-
-struct Monolevel * monolog_find_level_by_name(char * name) {
-  struct Monolevel key;
-  key.name    = name;
-  key.enabled = 123;
-  return dynar_bsearch(monolog->levels, &key, monolog_compare_loglevels); 
-}
-
-int monolog_setup_level(char * name, int enabled) {
-  struct Monolevel * level;
-  int levelindex;
-  level = monolog_find_level_by_name(name);
-  if (!level) { 
-    levelindex = monolog_get_unused_level();
-    if (levelindex < 0) return -1;
-    level = monolog_get_level(levelindex);
-  }
-  level->name    = name;
-  level->enabled = enabled;
-  dynar_qsort(monolog->levels, monolog_compare_loglevels);
-  return 1;
-}
-
-
-int monolog_enable_level(char * name) {
-  return monolog_setup_level(name, TRUE);
-}
-
-int monolog_disable_level(char * name) {
-  return monolog_setup_level(name, FALSE);  
-}
-
-int monolog_log_va(char * file, int line, char * name, char * format, va_list args) {
-  int index;
-  va_list args_copy;
-  struct Monolevel * level = monolog_find_level_by_name(name);
-  if ((!level) || (!level->enabled)) {
-    return -1;
-  }
-  
-  for (index = 0; index < dynar_size(monolog->loggers); index++) {
-    struct Monologger * logger = monolog_get_logger(index);
-    if (logger && logger->logger.log) {
-      va_copy(args_copy, args);
-      logger->logger.log(file, line, level->name, logger->data, format, args_copy);
-      va_end(args_copy);
-    }
-  }
-  return 0;
-}
-
-
-int monolog_log(char * file, int line, char * level, char * format, ...) {
-  int result;
-  va_list args;
-  va_start(args, format);
-  result = monolog_log_va(file, line, level, format, args);
-  va_end(args);
-  return result;
-}
-
-
-/* Log function for logger that logs to stdout. */
-int monolog_stdout_logger
-  (char * file, int line, char * level, void * data, char * format, va_list args)
-{
-  (void) data;
-  printf("%s: %s: %d: ", level, file, line);
-  return vprintf(format, args);
-}
-
-/* Log function for logger that logs to stderr. */
-int monolog_stderr_logger
-  (char * file, int line, char * level, void * data, char * format, va_list args)
-{
-  (void) data;
-  fprintf(stderr, "%s: %s: %d: ", level, file, line);
-  return vfprintf(stderr, format, args);
-}
-
-
-/** Log function for logger that logs to a FILE. */
-int monolog_file_logger
-(char * file, int line, char * level, void * data, char * format, va_list args) {
-  FILE * fout = data;
-  int res;
-  if (fout) {
-    fprintf(fout, "%s: %s: %d: ", level, file, line);
-    res =  vfprintf(fout, format, args);
-    fflush(fout);
-    return res;
-  }
-  return -1;
-}
-
-
-/** Free function for logger that logs to a FILE. */
-void monolog_file_free(void * data) {
-  FILE * fout = data;
-  if (fout) {
-    fclose(fout);
-  }
-}
-
-
-
-
-
-
-

+ 0 - 481
oldsrc/rh.c

@@ -1,481 +0,0 @@
-  
-#include "rh.h"
-#include "mem.h"
-#include "monolog.h"
-#include "state.h"
-
-#include <string.h>
-
-#include <stdarg.h>
-#include <mruby.h>
-#include <mruby/error.h>
-#include <mruby/variable.h>
-
-#include "esh.h"
-#include "config.h"
-
-/* Debugging level for console output. */ 
-#define LOG_CONSOLE(FORMAT, ...) LOG_LEVEL("CONSOLE", FORMAT, __VA_ARGS__)
-#define LOG_ENABLE_CONSOLE()     LOG_ENABLE(CONSOLE)
-#define LOG_DISABLE_CONSOLE()    LOG_DISABLE(CONSOLE)
-
-/* Amount of parameters a mruby function can be called with using _va functions */
-#define RH_ARGS_MAX 64
-
-/*
-* RH contains helper functions for the mruby ruby interpreter.
-*/
-
-/*
- * Converts C like arguments to an array of mruby mrb_values.
- * Returns amount of arguments parsed, negative on failure.
- * 
- * Format specifier characters for the format argument: 
- *
- * z: String from null terminated C string      [char*]
- * s: String                                    [char*, int] 
- * Y: sYmbol from null terminated C string      [char*]
- * y: symbol                                    [char*, int] 
- * 
- * f: Float                                     [double]
- * i: Integer                                   [int]
- * b: Boolean                                   [int] 
- * 0: nil
- * Other characters: Error. Will return -(index or erroneous character) .
- */ 
-int rh_args_va(Ruby * ruby, mrb_value * values,  int size,  char * format, va_list list) {
-  int ch; int index;
-  int i; double d; const char * str;
-  index = 0; 
-  
-  for (ch = (*format) ; (ch) && (index < size) ; format++ , ch = (*format)) {
-    mrb_value val;
-    int error = FALSE;
-    switch(ch) {
-      case 's':
-        str = va_arg(list, const char*);
-        i   = va_arg(list, int);
-        val = mrb_str_new(ruby, str, i);
-        break;
-        
-      case 'z':
-        str = va_arg(list, const char*);
-        val = mrb_str_new_cstr(ruby, str);
-        break;
-        
-      case 'y':
-        str = va_arg(list, const char*);
-        i   = va_arg(list, int);
-        val = mrb_symbol_value(mrb_intern(ruby, str, (size_t)i));
-        break;
-        
-      case 'Y':
-        str = va_arg(list, const char*);
-        val = mrb_symbol_value(mrb_intern_cstr(ruby, str));
-        break; 
-        
-      case 'i':
-        i   = va_arg(list, int);
-        val = mrb_fixnum_value(i);
-        break;
-        
-      case 'f':
-        d   = va_arg(list, double);
-        val = mrb_float_value(ruby, d);
-        break;
-        
-      case 'b': 
-        i   = va_arg(list, int);
-        val = ( i ? mrb_true_value() : mrb_false_value());
-        break;
-      
-      case '0':
-        val = mrb_nil_value();
-        break;
-        
-      default:
-        error = TRUE;
-        break;
-    }
-    if (error) { 
-      return -index;
-    }
-    values[index] = val;
-    index ++;
-  }
-  return index;
-}
-
-
-/* strdup isn't ANSI C, just posix... :p so we need our own version.*/
-char *rh_strdup(const char *str) {
-    char * res = malloc(strlen(str) + 1);
-    if(res) { strcpy(res, str); }
-    return res;
-}
-
-/* Helps convert C values to mruby values in an array. */
-int rh_args(Ruby * ruby, mrb_value * values,  int size,  char * format, ...) {
-  int res;
-  va_list list;
-  va_start(list, format);
-  res = rh_args_va(ruby, values, size, format, list);
-  va_end(list);
-  return res;
-}
-
-
-/** Calulates the execption string. Result only tempoarily available..
-XXX: check if this doesn't leak memory... you must free the results manually.
-*/
-char * rh_exceptionstring(Ruby * self) {
-  char      * result;
-  mrb_value   value;
-  mrb_value   backtrace;
-  mrb_value   backtrace_str;
-  
-  if (!self->exc) return NULL; 
-  //
-  /* XXX: Too bad, the backtrace doesn't seem to be filled in for some reason...
-   * Should figure out how to fix this.
-   */
-  mrb_print_backtrace(self);
-  backtrace = // mrb_get_backtrace(self);
-  mrb_funcall(self, mrb_obj_value(self->exc), "backtrace", 0);
-
-  backtrace_str = mrb_funcall(self, backtrace, "join", 1, mrb_str_new_lit(self, "\n"));
-  LOG_ERROR("backtrace: %s\n", mrb_string_value_cstr(self, &backtrace_str));
-  value  = mrb_funcall(self, mrb_obj_value(self->exc), "inspect", 0);
-  
-  
-  
-  // reset exception since we have it's string value.
-  // Does this leak memory or not???
-  self->exc = NULL;
-  return rh_strdup(mrb_string_value_cstr(self, &value));
-}
-
-
-/** Allocates and initialzes a new ruby state. */
-Ruby * rh_new() {
-   Ruby * self = mrb_open();
-   /*mrb_define_method(self, self->kernel_module, 
-                     "path", tr_Path, ARGS_REQ(1));*/
-   return self;
-}
-
-/** Frees a ruby state. */
-Ruby * rh_free(Ruby * self) {
-  mrb_close(self);
-  return NULL;
-}
-
-/** Returns an mrb_value that contains the value of object.inspect. 
-*/
-mrb_value rh_inspect(mrb_state *mrb, mrb_value obj) {
-  return mrb_inspect(mrb, obj);
-}
-
-char * rh_inspect_cstr(mrb_state *mrb, mrb_value value) {
-  mrb_value res = rh_inspect(mrb, value);
-  /* XXX: check that it's not modified anywere or export the const? */
-  return (char *) mrb_string_value_cstr(mrb, &res);
-}
-
-
-/* Does the actual reporting depending on the current state of 
- ruby and the returned value. */
-int rh_make_report(Ruby * self, mrb_value v) { 
-  int res = 0;
-  char * str;
- 
-  /* Report exceptions */
-  str = rh_exceptionstring(self);
-  if(str) {
-    LOG_WARNING("mruby exception: %s\n", str);
-    free(str);
-    return 0;
-  }
-  
-  /* Report result value if it's not nil on debug and console levels.
-   */
-  if (!mrb_nil_p(v)) {
-    str = rh_inspect_cstr(self, v);
-    LOG_DEBUG("mruby result: %s\n", str);
-    LOG_CONSOLE("-> %s\n", str);
-    return 0;
-  }
-  return 0;
-}
-
-
-/* Runs a file and reports any errors over the monolog logging system. */
-int rh_run_file(Ruby * self, const char * filename, FILE * file) {
-  int res;
-  char * str;
-  mrbc_context * c ; 
-  mrb_value v;
-  int ai;
-  ai = mrb_gc_arena_save(self);
-  c = mrbc_context_new(self);  
-  mrbc_filename(self, c, filename);
-  v = mrb_load_file_cxt(self, file, c);
-  mrbc_context_free(self, c);  
-  /* Report exceptions */
-  res = rh_make_report(self, v);  
-  mrb_gc_arena_restore(self, ai);
-  return res;
-}
-
-int rh_run_filename(Ruby * self, const char * filename) {
-  FILE * file = fopen(filename, "rt");
-  int res;
-  if (!file) {
-    LOG_ERROR("No such ruby file: %s\n", filename);
-    return -1;
-  }
-  res = rh_run_file(self, filename, file);
-  fclose(file);
-  return 0;
-}
-
-
-/**
-* Executes a ruby file in Eruta's data/script directory with reporting.
-* Returns -2 if the file was not found.
-* Returns -3 if the path wasn't found.
-*/
-int rh_run_script(Ruby * self, const char * filename) {
-  struct woesb path = {0};
-  int runres;
-  
-  if (!woesb_new_join(&path, MRB_WOE_CONFIG(self)->data_dir, "/script/", filename, NULL)) {
-    woesb_free(&path);
-    LOG_ERROR("Out of memory when joining path.\n");
-    return -3;
-  }
-  
-  if (strstr(path.text, "..")) {
-    woesb_free(&path);
-    LOG_ERROR("Path may not contain '..' \n");
-    return -4;
-  }
-  
-  runres = rh_run_filename(self, path.text);
-  woesb_free(&path);
-  return runres;
-}
-
-
-/* Executes a ruby command string. 
-Errors are reported to the reporter callback if it isn't NULL. */
-int rh_dostring(Ruby * self, const char * command) {
-  int res = 0;
-  char * str;
-  mrb_value v;
-  int ai;
-  ai = mrb_gc_arena_save(self);  
-  #ifdef RH_USE_CONTEXT
-  mrbc_context      * c  = mrbc_context_new(self);
-  mrbc_filename(self, c, "command");
-  v = mrb_load_string_cxt(self, command, c);
-  mrbc_context_free(self, c); 
-  #else
-  v = mrb_load_string(self, command);
-  #endif
-  /* Report exceptions */
-  res = rh_make_report(self, v);
-  /* mrb GC area seems to be an area for 1024 "new" objects for the generational 
-   * GC. It can overflow if a lot of new objects are generated 
-   * (say exceptions, etc) on the C side. To prevent this the area must be saved 
-   * and restored anywhere many ruby objects may have been generated.
-   * It seems that here too this is needed.
-   */
-  mrb_gc_arena_restore(self, ai);
-  return res;
-}
-
-
-/* Executes a ruby function with parameters. 
-Errors are reported to the reporter callback if it isn't NULL. */
-mrb_value rh_run_function_args(Ruby * self, mrb_value rubyself, char * funcname,    
-                        int argc, mrb_value * argv) {
-  int res = 0;
-  char * str;
-  mrb_value v;
-  mrb_sym symname = mrb_intern_cstr(self, funcname);
-  int ai;
-  if(!mrb_respond_to(self, rubyself, symname)) {
-    return mrb_nil_value();
-  }  
-  
-  ai = mrb_gc_arena_save(self);
-  v = mrb_funcall_argv(self, rubyself, symname, argc, argv);
-  res = rh_make_report(self, v);
-  mrb_gc_arena_restore(self, ai);
-  return v;
-}
-
-
-/** Runs a function in the ruby interpreter, with C arguments according to the 
- * given format string, logging results and errors back to
- * the reporter. The limit is RH_ARGS_MAX arguments.
- */
-mrb_value 
-rh_run_function_va(Ruby * self, mrb_value rubyself, char * funcname,
-                        char * format, va_list list) {
-  mrb_value argv[RH_ARGS_MAX];
-  int argc;
-  argc = rh_args_va(self, argv, RH_ARGS_MAX, format, list);
-  if (argc < 0) return mrb_nil_value();
-  return rh_run_function_args(self, rubyself, funcname, argc, argv);
-}
-
-
-/** Runs a function in the ruby interpreter, under the toplevel self. 
- * This logs results and errors using monolog.h interfaces.
- */
-mrb_value rh_run_toplevel_args(Ruby * ruby, char * name, int argc, mrb_value * argv) {
-  return rh_run_function_args(ruby, mrb_top_self(ruby), name, argc, argv);
-} 
-
-
-/** Runs a function in the ruby interpreter, under the toplevel self. 
- * This logs results and errors using monolog.h interfaces.
- */
-mrb_value rh_run_toplevel_va(Ruby * ruby, char * name, char * format, va_list list) {
-  return rh_run_function_va(ruby, mrb_top_self(ruby), name, format, list);
-} 
-
-
-/** Runs a function in the ruby interpreter, logging results and errors
- * using monolog.h interfaces.
- */
-mrb_value rh_run_function(Ruby * ruby, mrb_value rubyself, 
-                          char * name, char * format, ...) {
-  mrb_value res; 
-  va_list list;
-  va_start(list, format);
-  res = rh_run_function_va(ruby, rubyself, name, format, list);
-  va_end(list);
-  return res;
-}
-
-/** Runs a function in the ruby interpreter, under the toplevel self. 
- * This logs results and errors using monolog.h interfaces.
- */
-mrb_value rh_run_toplevel(Ruby * ruby, char * name, char * format, ...) {
-  mrb_value res; 
-  va_list list;
-  va_start(list, format);
-  res = rh_run_function_va(ruby, mrb_top_self(ruby), name, format, list);
-  va_end(list);
-  return res;
-}
-
-
-/* Calls a function, doesn't log anything. */
-mrb_value rh_simple_funcall(Ruby * ruby, char * name) {
-  int ai;  
-  Ruby * mrb = (Ruby *) ruby;  
-  mrb_value args[16];
-  ai = mrb_gc_arena_save(mrb);
-  LOG("GC Area: %d\n", ai);
-  // if(ai> 99) exit(0);
-  mrb_value v = mrb_funcall_argv(mrb, mrb_top_self(mrb), mrb_intern_cstr(mrb, name), 
-                    0, args);
-  
-  if (mrb->exc) {
-    if (!mrb_undef_p(v)) {        
-      mrb_p(mrb, mrb_obj_value(mrb->exc));
-    }
-    return mrb_nil_value();
-  }
-  mrb_gc_arena_restore(mrb, 0);
-  return v;
-}
-
-/* Maybe wrap this too? 
-mrb_value mrb_funcall_argv(mrb_state*, mrb_value, mrb_sym, int, mrb_value*);
-*/
-
-/* Converts and mrb_value to a C boolean as per the ruby interpretation. */
-int rh_tobool(mrb_value v) {
-  if (mrb_nil_p(v))             return FALSE;
-  return mrb_bool(v);
-}
-
-/* Documentation for mrb_get_args
-  retrieve arguments from mrb_state.
-
-  mrb_get_args(mrb, format, ...)
-  
-  returns number of arguments parsed.
-
-  format specifiers:
-
-   o: Object [mrb_value]
-   S: String [mrb_value]
-   A: Array [mrb_value]
-   H: Hash [mrb_value]
-   s: String [char*,int]
-   z: String [char*]
-   a: Array [mrb_value*,int]
-   f: Float [mrb_float]
-   i: Integer [mrb_int]
-   n: Symbol [mrb_sym]
-   &: Block [mrb_value]
-   *: rest argument [mrb_value*,int]
-   |: optional
-*/
-
-
-
-/* Stores a pointer in the mrb state. Handy to avoid having to use global 
- * variables o,n the C side. It's done by adding a ruby global variable
- * with a name that cannot be used from the mruby side. */
-void * rh_store_pointer(mrb_state * mrb, const char * name, void * ptr) {
-  struct woesb hname;
-  mrb_value      val;
-  mrb_sym      rname;
-
-  if (!woesb_new_join(&hname, "--", name, "--", NULL)) {
-    woesb_free(&hname);
-    LOG_ERROR("Out of memory when making hidden name.\n");
-    return NULL;
-  }
-  
-  rname = mrb_intern_cstr(mrb, hname.text);
-  val   = mrb_cptr_value(mrb, ptr);  
-  mrb_gv_set(mrb, rname, val);
-  woesb_free(&hname);
-  return ptr;
-}
-
-/** Fetches a previously stored pointer. */
-void * rh_fetch_pointer(mrb_state * mrb, const char * name) {
-  struct woesb hname;
-  mrb_value      val;
-  mrb_sym      rname;
-
-  if (!woesb_new_join(&hname, "--", name, "--", NULL)) {
-    woesb_free(&hname);
-    LOG_ERROR("Out of memory when making hidden name.\n");
-    return NULL;
-  }
-  
-  rname = mrb_intern_cstr(mrb, hname.text);
-  val   = mrb_gv_get(mrb, rname);
-  woesb_free(&hname);
-  return mrb_cptr(val);
-}
-
-
-/* Tries to (re-)load the main ruby file, output to console. */
-int rh_load_main() { 
-  return -1; /*rh_run_script(woe_global_ruby, "main.rb"); */
-}
-
-
-
-

+ 0 - 850
oldsrc/server.c

@@ -1,850 +0,0 @@
-# if !defined(_POSIX_SOURCE)
-#   define _POSIX_SOURCE
-# endif
-# if !defined(_BSD_SOURCE)
-#   define _BSD_SOURCE
-# endif
-
-# include <sys/socket.h>
-# include <netinet/in.h>
-# include <arpa/inet.h>
-# include <netdb.h>
-# include <poll.h>
-# include <unistd.h>
-
-
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <ctype.h>
-
-#include "client.h"
-#include "timer.h"
-#include "server.h"
-#include "monolog.h"
-
-#include "libtelnet.h"
-
-#include "rh.h"
-
-
-static const telnet_telopt_t woe_telnet_opts[] = {
-
-  { TELNET_TELOPT_COMPRESS2,  TELNET_WILL, TELNET_DONT },
-  { TELNET_TELOPT_ECHO     ,  TELNET_WONT, TELNET_DONT },
-
-  { -1, 0, 0 }
-};
-
-
-#define WOE_SERVER_BUFFER_SIZE      10000
-#define WOE_SERVER_LISTEN_BACKLOG   8
-
-
-struct woe_server {
-  char                buffer[WOE_SERVER_BUFFER_SIZE];
-  short               listen_port;
-  int                 listen_sock;
-  int                 busy;
-  struct sockaddr_in  addr;
-  socklen_t           addrlen;
-  mrb_state         * mrb;
-  
-  struct pollfd       pfd[WOE_CLIENTS_MAX + 1];
-  struct woe_client * clients[WOE_CLIENTS_MAX];  
-  struct woe_timer  * timers[WOE_TIMERS_MAX]; 
-  
-  void (*event_handler)      (telnet_t *telnet, telnet_event_t *ev, void *user_data);
-  void (*disconnect_handler) (struct woe_server * srv, struct woe_client * cli, void *user_data);
-};
-
-
-
-int woe_server_disconnect(struct woe_server * srv, struct woe_client * client);
-
-
-int woe_send(int sock, const char *buffer, unsigned int size) {
-  int res;
-
-  if (sock == -1)
-    return -1;
-
-  /* send data */
-  while (size > 0) {
-    if ((res = send(sock, buffer, size, 0)) == -1) {
-      if (errno != EINTR && errno != ECONNRESET) {
-        LOG_ERROR("send() failed: %s\n", strerror(errno));
-        return -2;
-      } else {
-        return 0;
-      }
-    } else if (res == 0) {
-      LOG_ERROR("send() unexpectedly returned 0\n");
-      return -3;
-    }
-
-    /* update pointer and size to see if we've got more to send */
-    buffer += res;
-    size -= res;
-  }
-  return 0;
-}
-
-
-
-static void woe_event_handler(telnet_t *telnet, telnet_event_t *ev, void *user_data) {
-  struct woe_client * client = (struct woe_client *) user_data;
-
-  switch (ev->type) {
-  /* data received */
-  case TELNET_EV_DATA:
-    woe_client_input(client, ev->data.buffer, ev->data.size);
-    /* telnet_negotiate(telnet, TELNET_WONT, TELNET_TELOPT_ECHO);
-      telnet_negotiate(telnet, TELNET_WILL, TELNET_TELOPT_ECHO); */
-    break;
-    
-  /* data must be sent */
-  case TELNET_EV_SEND:
-    woe_send(client->sock, ev->data.buffer, ev->data.size);
-    break;
-        
-  /* enable compress2 if accepted by client */
-  case TELNET_EV_DO:
-    if (ev->neg.telopt == TELNET_TELOPT_COMPRESS2) telnet_begin_compress2(telnet);
-    woe_client_negotiate(client, TELNET_DO, ev->neg.telopt);
-    break;
-
-
-  case TELNET_EV_DONT:
-    woe_client_negotiate(client, TELNET_DONT, ev->neg.telopt);
-    break;
-    
-  case TELNET_EV_WILL:  
-    woe_client_negotiate(client, TELNET_WILL, ev->neg.telopt);
-    break;
-    
-  case TELNET_EV_WONT:  
-    woe_client_negotiate(client, TELNET_WONT, ev->neg.telopt);
-    break;
-    
-  case TELNET_EV_IAC:
-    woe_client_iac(client, ev->iac.cmd);
-    break;
-    
-  case TELNET_EV_SUBNEGOTIATION:
-    woe_client_subnegotiate(client, ev->sub.buffer, ev->sub.size, ev->sub.telopt);
-    break; 
-
-  case TELNET_EV_TTYPE:
-    woe_client_ttype(client, ev->ttype.cmd, ev->ttype.name);
-    break;
-    
-  case TELNET_EV_COMPRESS: 
-    woe_client_compress(client, ev->compress.state);
-    break;
-  
-  case TELNET_EV_ENVIRON:
-    woe_client_environ(client, ev->environ.cmd, ev->environ.values, ev->environ.size);
-    break;
-  
-  case TELNET_EV_MSSP:
-    woe_client_mssp(client, ev->mssp.values, ev->mssp.size);
-    break; 
-
-  /* warning */
-  case TELNET_EV_WARNING:  
-    LOG_WARNING("Telnet warning for client %d %s.\n", client->index, ev->error.msg);    
-    woe_client_warning(client, ev->error.errcode, ev->error.msg);
-    break;
-  
-  /* error */
-  case TELNET_EV_ERROR:  
-    LOG_ERROR("Telnet error for client %d %s.\n", client->index, ev->error.msg);    
-    woe_client_error(client, ev->error.errcode, ev->error.msg);
-    woe_server_disconnect(client->server, client);
-    break;
-  
-  case TELNET_EV_ZMP:
-    woe_client_zmp(client, ev->zmp.argc, ev->zmp.argv);
-    break;
-     
-  default:
-     LOG_NOTE("Ignored telnet event %d.\n", ev->type);
-    /* ignore */
-    break;
-  }
-}
-
-
-void woe_server_request_shutdown(struct woe_server * srv) {  
-  if (srv) srv->busy = 0;
-}
-
-int woe_server_busy(struct woe_server * srv) {
-  if (!srv) return 0;
-  return srv->busy;
-}
-
-
-struct woe_server * woe_server_free(struct woe_server * srv) {
-  int index;
-  
-  close(srv->listen_sock);
- 
-  for (index = 0; index < WOE_CLIENTS_MAX; ++index) {
-     woe_server_remove_client(srv, index);
-  }
-
-  for (index = 0; index < WOE_TIMERS_MAX; ++index) {
-     woe_server_remove_timer(srv, index);
-  }
-
-  free(srv);
-  return NULL;
-}
-
-
-
-struct woe_server * woe_server_init(struct woe_server * srv, int port) {
-  int index;
-  if (!srv) return NULL;
-  srv->listen_sock = -1;
-  srv->listen_port = port;
-  srv->busy        = !0;
-  srv->mrb         = NULL;
-  memset(srv->buffer  , '\0'  , sizeof(srv->buffer));
-  memset(srv->pfd     , 0     , sizeof(srv->pfd));
-  memset(&srv->addr   , 0     , sizeof(srv->addr));
-  
-  for (index = 0; index < WOE_CLIENTS_MAX; ++index) {
-     srv->clients[index] = NULL;
-  }
-
-  for (index = 0; index < WOE_TIMERS_MAX; ++index) {
-     srv->timers[index] = NULL;
-  }
-  
-   srv->event_handler = woe_event_handler;
-  return srv;
-}
-
-struct woe_server * woe_server_new(int port) {
-  struct woe_server * srv = calloc(1, sizeof(struct woe_server));
-  if (!srv) return NULL;  
-  return woe_server_init(srv, port);
-}
-
-
-static const telnet_telopt_t telopts[] = {
-  { TELNET_TELOPT_COMPRESS2,  TELNET_WILL, TELNET_DONT },
-  { -1, 0, 0 }
-};
-
-
-struct woe_server * woe_server_set_mrb(struct woe_server * srv, mrb_state * mrb) {
-  if (!srv) return NULL;  
-  srv->mrb = mrb;
-  return srv;
-}
-
-mrb_state * woe_server_get_mrb(struct woe_server * srv) {
-  if (!srv) return NULL;
-  return srv->mrb;
-}
-
-
-/** Sets up the server to listen at its configured port. */
-int woe_server_listen(struct woe_server * srv) {
-  int         res;
-  /* create listening socket */
-  if ((srv->listen_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
-    LOG_ERROR("socket() failed: %s\n", strerror(errno));
-    return 1;
-  }
-
-  /* Reuse address option */
-  res = 1;
-  if (setsockopt(srv->listen_sock, SOL_SOCKET, SO_REUSEADDR, (void*)&res, sizeof(res))
-     == -1) {
-    LOG_ERROR("setsockopt() failed: %s\n", strerror(errno));
-    return 2;
-  }
-     
-  /* Bind to listening addr/port */  
-  srv->addr.sin_family       = AF_INET;
-  srv->addr.sin_addr.s_addr  = INADDR_ANY;
-  srv->addr.sin_port         = htons(srv->listen_port);
-  if (bind(srv->listen_sock, (struct sockaddr *)&srv->addr, sizeof(srv->addr)) == -1) {
-    LOG_ERROR("setsockopt() failed: %s\n", strerror(errno));
-    return 3;
-  }
-
-  /* listen for clients */
-  if (listen(srv->listen_sock, WOE_SERVER_LISTEN_BACKLOG) == -1) {
-    fprintf(stderr, "listen() failed: %s\n", strerror(errno));
-    return 4;
-  }
-
-  LOG_NOTE("Listening on port %d\n", srv->listen_port);
-
-  /* Initialize listening descriptors */
-  srv->pfd[WOE_CLIENTS_MAX].fd = srv->listen_sock;
-  srv->pfd[WOE_CLIENTS_MAX].events = POLLIN;
-  
-  return 0;
-}
-
-/** Returns one of the clients of the server or NULL if not in use or out of 
- * range */
-struct woe_client * woe_server_get_client(struct woe_server * srv, int index) {
-  if (!srv)                           return NULL;
-  if (index < 0)                      return NULL;
-  if (index >= WOE_CLIENTS_MAX)       return NULL;
-  return srv->clients[index];
-}
-
-/** Stores a client of the server at the given index*/
-struct woe_client * woe_server_put_client(struct woe_server * srv, int index, struct woe_client * cli) {
-  if (!srv)                           return NULL;
-  if (!cli)                           return NULL;
-  if (index < 0)                      return NULL;
-  if (index >= WOE_CLIENTS_MAX)       return NULL;
-  if (srv->clients[index]) {
-    woe_client_free(srv->clients[index]);
-  }  
-  srv->clients[index] = cli;
-  return cli; 
-}
-
-/** Removes a client of the server at the given index*/
-struct woe_client * woe_server_remove_client(struct woe_server * srv, int index) {
-  if (!srv)                           return NULL;
-  if (index < 0)                      return NULL;
-  if (index >= WOE_CLIENTS_MAX)       return NULL;
-  if (srv->clients[index]) {
-    woe_client_free(srv->clients[index]);
-  }  
-  srv->clients[index] = NULL;
-  return NULL; 
-}
-
-
-/** Returns one of the timers of the server or NULL if not in use or out of 
- * range */
-struct woe_timer * woe_server_get_timer(struct woe_server * srv, int index) {
-  if (!srv)                           return NULL;
-  if (index < 0)                      return NULL;
-  if (index >= WOE_CLIENTS_MAX)       return NULL;
-  return srv->timers[index];
-}
-
-/** Stores a timer of the server at the given index*/
-struct woe_timer * woe_server_put_timer(struct woe_server * srv, int index, struct woe_timer * tim) {
-  if (!srv)                           return NULL;
-  if (!tim)                           return NULL;
-  if (index < 0)                      return NULL;
-  if (index >= WOE_TIMERS_MAX)        return NULL;
-  if (srv->timers[index]) {
-    woe_timer_free(srv->timers[index]);
-  }  
-  srv->timers[index] = tim;
-  return tim; 
-}
-
-/** Removes a timer of the server at the given index*/
-struct woe_timer * woe_server_remove_timer(struct woe_server * srv, int index) {
-  if (!srv)                           return NULL;
-  if (index < 0)                      return NULL;
-  if (index >= WOE_TIMERS_MAX)        return NULL;
-  if (srv->timers[index])  {
-    woe_timer_free(srv->timers[index]);
-  }  
-  srv->timers[index] = NULL;
-  return NULL; 
-}
-
-
-/** Find an index to put a newtimerr and returns a pointer to it. 
- *  Returns -1 if no free space is available. 
- **/
-int woe_server_get_available_timer_index(struct woe_server * srv) {
-   int i;
-   for (i = 0; i < WOE_TIMERS_MAX; ++i) {
-     struct woe_timer * timer = woe_server_get_timer(srv, i);
-     if (!timer) {
-       return i;
-     } 
-   }
-   return -1;
-}
-
-/** Creates a new timer for this server. Return null if no memory or no space 
- * for a new timer. */
-struct woe_timer * woe_server_make_new_timer(struct woe_server * srv) {
-   struct woe_timer * new;
-   int index = woe_server_get_available_timer_index(srv);
-   if (index < 0) return NULL;
-   new = woe_timer_new(srv, index);
-   return woe_server_put_timer(srv, index, new);
-} 
-
-
-/** Creates a new timr and returns it's id. */
-int woe_server_make_new_timer_id(struct woe_server * srv) {
-   struct woe_timer * new = woe_server_make_new_timer(srv);
-   if (!new) return -1;
-   return new->index;
-}
-
-
-
-/** Find an index to put a new user and returns a pointer to it. 
- *  Returns -1 if no free space is available. 
- **/
-int woe_server_get_available_client_index(struct woe_server * srv) {
-   int i;
-   for (i = 0; i < WOE_CLIENTS_MAX; ++i) {
-     struct woe_client * client = woe_server_get_client(srv, i);
-     if (!client) {
-       return i;
-     } 
-   }
-   return -1;
-}
-
-/** Creates a new client for this server. Return null if no memory or no space 
- * for a new client. */
-struct woe_client * woe_server_make_new_client(struct woe_server * srv, 
-  int socket, struct sockaddr_in *addr, socklen_t addrlen) {
-   struct woe_client * new;
-   int index = woe_server_get_available_client_index(srv);
-   if (index < 0) return NULL;
-   new = woe_client_new(srv, index, socket, addr, addrlen);
-   return woe_server_put_client(srv, index, new);
-} 
-
-
-
-/** Sets a timer's values by id */
-int woe_server_set_timer_value(struct woe_server * srv, int index, double value, double interval) {
-  struct woe_timer * tim;
-  tim = woe_server_get_timer(srv, index);
-  if (!tim) return -1;
-  return woe_timer_set(tim, value, interval);
-}
-
-/** Gets a timer's values by id */
-int woe_server_get_timer_value(struct woe_server * srv, int index, double * value, double * interval) {
-  struct woe_timer * tim;
-  tim = woe_server_get_timer(srv, index);
-  if (!tim) return -1;
-  return woe_timer_get(tim, value, interval);
-}
-
-
-/* Handles a new connection to this server. */
-int woe_server_handle_connect(struct woe_server * srv) {
-  struct woe_client * client;
-  int res;
-  struct sockaddr_in addr;
-  socklen_t addrlen;
- 
-  /* accept the connection */
-  addrlen = sizeof(addr);
-  if ((res = accept(srv->listen_sock, (struct sockaddr *)&addr, &addrlen)) == -1) {
-    LOG_ERROR("accept() failed: %s\n", strerror(errno));
-    return 1;
-  }
-  
-  LOG_NOTE("Connection received.\n");
-  
-  client = woe_server_make_new_client(srv, res, &addr, addrlen);
-  
-  /* No space for a new client */
-  if (!client) {
-    LOG_WARNING("Connection rejected (too many users or OOM)\n");
-    write(res, "Too many users.\r\n", 14);
-    close(res);  
-    return 2;
-  }
-
-  /* init, welcome */
-  client->telnet  = telnet_init(woe_telnet_opts, srv->event_handler, 0, client);
-  if (!client->telnet) {
-    LOG_ERROR("Could not initialize telnet connection for user.");
-    woe_server_disconnect(srv, client);
-    return 3;    
-  }
-  /*telnet_negotiate(client->telnet, TELNET_DO, TELNET_TELOPT_ECHO);*/
-  telnet_negotiate(client->telnet, TELNET_WILL, TELNET_TELOPT_COMPRESS2);
-  telnet_printf(client->telnet, "Welcome to WOE!\r\n");
-  /* telnet_negotiate(client->telnet, TELNET_WILL, TELNET_TELOPT_ECHO); */
-  
-  if (srv->mrb) {
-    rh_run_toplevel(srv->mrb, "woe_on_connect", "i", client->index);
-  }
-  
-  return 0;
-}
-
-
-/** Sends a telnet command to the numbered client. */
-int woe_server_iac(struct woe_server * srv, int client, int command) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_iac(pclient->telnet, command);
-  return 0;
-}
-
-
-/** Send a telnet negotiation to the numbered client. */
-int woe_server_negotiate(struct woe_server * srv, int client, int how, int option) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_negotiate(pclient->telnet, how, option);
-  return 0;
-}
-
-/** Sends a telnet start of subnegotiation to the numbered client. */
-int woe_server_begin_sb(struct woe_server * srv, int client, int telopt) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_begin_sb(pclient->telnet, telopt);
-  return 0;
-}
-
-/** Sends a telnet end of subnegotiation to the numbered client. */
-int woe_server_finish_sb(struct woe_server * srv, int client) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_finish_sb(pclient->telnet);
-  return 0;
-}
-
-
-/** Sends a complete telnet subnegotiation  buffer to the numbered client. */
-int woe_server_subnegotiation(struct woe_server * srv, int client, int telopt, char * buffer, int size) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_subnegotiation(pclient->telnet, telopt, buffer, size);
-  return 0;
-}
-
-/** Begin sending compressed data to the to the numbered client. */
-int woe_server_begin_compress2(struct woe_server * srv, int client) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_begin_compress2(pclient->telnet);
-  return 0;
-}
-
-
-/** Send formated output with newline escaping to the to the numbered client. */
-int woe_server_vprintf(struct woe_server * srv, int client, const char *fmt, va_list va) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_vprintf(pclient->telnet, fmt, va);  
-  return 0;
-}
-
-/** Send formated output with newline escaping to the to the numbered client. */
-int woe_server_printf(struct woe_server * srv, int client, const char *fmt, ...) {
-  va_list va;
-  int res;
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  va_start(va, fmt);
-  telnet_vprintf(pclient->telnet, fmt, va);
-  va_end(va);
-  return 0;
-}
-
-/** Send formated output without newline escaping to the to the numbered client. */
-int woe_server_raw_vprintf(struct woe_server * srv, int client, const char *fmt, va_list va) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_raw_vprintf(pclient->telnet, fmt, va);
-  return 0;  
-}
-
-/** Send formated output without newline escaping to the to the numbered client. */
-int woe_server_raw_printf(struct woe_server * srv, int client, const char *fmt, ...) {
-  va_list va;
-  int res;
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  va_start(va, fmt);
-  res = telnet_raw_vprintf(pclient->telnet, fmt, va);
-  va_end(va);
-  return 0;
-}
-
-
-/** Begin a NEW-ENVIRON subnegotiation with the numbered client. */
-int woe_server_begin_newenviron(struct woe_server * srv, int client, int type) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_begin_newenviron(pclient->telnet, type);
-  return 0;
-}
-
-/** Send a NEW-ENVIRON variable name or value to the numbered client. */
-int woe_server_newenviron_value(struct woe_server * srv, int client, int type, char * value) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_newenviron_value(pclient->telnet, type, value);
-  return 0;
-}
-
-/** Finish a NEW-ENVIRON subnegotiation with the numbered client. */
-int woe_server_finish_newenviron(struct woe_server * srv, int client) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_finish_newenviron(pclient->telnet);
-  return 0;
-}
-
-/** Send a TERMINAL-TYPE SEND command to the numbered client. */
-int woe_server_ttype_send(struct woe_server * srv, int client) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_ttype_send(pclient->telnet);
-  return 0;
-}
-
-/** Send a TERMINAL-TYPE IS command to the numbered client. */
-int woe_server_ttype_is(struct woe_server * srv, int client, char * ttype) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_ttype_is(pclient->telnet, ttype);
-  return 0;
-}
-
-
-/** Send a ZMP command to the numbered client. */
-int woe_server_send_zmp(struct woe_server * srv, int client, int argc, const char ** argv) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_send_zmp(pclient->telnet, argc, argv);
-  return 0;
-}
-
-/** Send a ZMP command to the numbered client. */
-int woe_server_send_vzmpv(struct woe_server * srv, int client, va_list va) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_send_vzmpv(pclient->telnet, va);
-  return 0;
-}
-
-/** Send a ZMP command to the numbered client. */
-int woe_server_send_zmpv(struct woe_server * srv, int client, ...) {
-  va_list va;
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  va_start(va, client);
-  telnet_send_vzmpv(pclient->telnet, va);
-  va_end(va);
-  return 0;
-}
-
-/** Begin sending a ZMP command to the numbered client. */
-int woe_server_begin_zmp(struct woe_server * srv, int client, const char * cmd) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_begin_zmp(pclient->telnet, cmd);
-  return 0;
-}
-
-
-/** Send a ZMP command argument to the numbered client. */
-int woe_server_zmp_arg(struct woe_server * srv, int client, const char * arg) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_zmp_arg(pclient->telnet, arg);
-  return 0;
-}
-
-/** Finish sending a ZMP command to the numbered client. */
-int woe_server_finish_zmp(struct woe_server * srv, int client, const char * cmd) {
-  struct woe_client * pclient;
-  if (!srv)     return -1;
-  pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -2;
-  telnet_finish_zmp(pclient->telnet);
-  return 0;
-}
-
-
-/** Disconnect a client from the server. */
-int woe_server_disconnect(struct woe_server * srv, struct woe_client * client) {
-  int index;
-  
-  if (!srv)     return 1;
-  if (!client)  return 2; 
-  close(client->sock);
-  if (srv->disconnect_handler) {
-    srv->disconnect_handler(srv, client, NULL);
-  }
-  index = client->index;
-  
-  if (srv->mrb) {
-    rh_run_toplevel(srv->mrb, "woe_on_disconnect", "i", index);
-  }
-    
-  /* Get rid of client, will also free memory asociated. */
-  woe_server_remove_client(srv, index);  
-  return 0;
-}
-
-
-/** Forcfullly disconnect a client from the server by id. 
- * Set a quit flag that woe_server_update will check. */
-int woe_server_disconnect_id(struct woe_server * srv, int id) {
-  struct woe_client * client = woe_server_get_client(srv, id);
-  if (!client) return -1;
-  client->busy = 0;
-  return 0;
-}
-
-/** Polls the server once and updates any of the clients if needed. */
-int woe_server_update(struct woe_server * srv, int timeout) {
-  int i, res;
-
-  /* Check timers for readiness. */
-  for (i = 0; i < WOE_TIMERS_MAX; ++i) {
-    struct woe_timer * timer = woe_server_get_timer(srv, i);
-    if (woe_timer_passed(timer)) {
-      woe_timer_callback(timer);
-    }
-  }
-
-  
-  /* Disconnect clients that should quit */
-  for (i = 0; i < WOE_CLIENTS_MAX; ++i) {
-    struct woe_client * client = woe_server_get_client(srv, i);
-    if (!client) continue;
-    if (!client->busy) {
-      woe_server_disconnect(srv, client);
-    }  
-  }
-
-  /* prepare for poll */
-  memset(srv->pfd     , 0     , sizeof(srv->pfd));
-  
-  
-  for (i = 0; i != WOE_CLIENTS_MAX; ++i) {
-   struct woe_client * client = woe_server_get_client(srv, i);
-   if (client) {
-     srv->pfd[i].fd     = client->sock;
-     srv->pfd[i].events = POLLIN;
-   } else {
-     srv->pfd[i].fd     = -1;
-     srv->pfd[i].events = 0;
-   }
-  }
-  
-  /* Also listen for connnect events. */
-  srv->pfd[WOE_CLIENTS_MAX].fd = srv->listen_sock;
-  srv->pfd[WOE_CLIENTS_MAX].events = POLLIN;
-  
-  
-
-  /* Poll for activity */
-  res = poll(srv->pfd, WOE_CLIENTS_MAX + 1, timeout);
-
-  /* Check for time out */
-  if (res == 0) {
-   /* Time out but that's OK. */
-   return 0;
-  }
-
-  /* Log errors. */
-  if (res == -1 && errno != EINTR) {
-    LOG_ERROR("poll() failed: %s\n", strerror(errno));
-    return 1;
-  }
-
-  /* Handle new connection connection */
-  if (srv->pfd[WOE_CLIENTS_MAX].revents & POLLIN) {
-    woe_server_handle_connect(srv);
-  }
-
-   /* Read from clients */
-  for (i = 0; i < WOE_CLIENTS_MAX; ++i) {
-    struct woe_client * client = woe_server_get_client(srv, i);
-    if (!client) continue;
-    
-    /* Input from clients. */
-    if (srv->pfd[i].revents & POLLIN) {
-      res = recv(client->sock, srv->buffer, sizeof(srv->buffer), 0);
-      if (res < 0) {
-        LOG_ERROR("recv(client) failed: %s\n", strerror(errno));
-      } else if (res == 0) {
-        /* Disconnect the client. */
-        woe_server_disconnect(srv, client);
-      } else {
-        /* Let telnet lib process incoming data. */
-        telnet_recv(client->telnet, srv->buffer, res);
-        // telnet_send(client->telnet, srv->buffer, res);
-        // telnet_send(telnet, ev->data.buffer, ev->data.size);
-      }
-    }
-  }
-  
-  
-    
-  return 0;
-}
-
-
-int woe_server_send_to_client(struct woe_server * srv, int client, char * data, size_t size) {
-  struct woe_client * pclient = woe_server_get_client(srv, client);
-  if (!pclient) return -1;
-  telnet_send(pclient->telnet, data, size);
-  return size;
-}
-
-
-

+ 0 - 139
oldsrc/timer.c

@@ -1,139 +0,0 @@
-/**
- * This file client.c, handles clients of the WOE server.
- */
-
-#define _POSIX_C_SOURCE 200801L
-#define _POSIX_SOURCE 200801L
-
-#include <signal.h>
-#include <time.h>
-
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <math.h>
-
-#include "libtelnet.h"
-#include "monolog.h"
-#include "rh.h"
-#include "timer.h"
-#include "server.h"
-
-
-
-struct woe_timer * woe_timer_alloc() {
-  return calloc(sizeof(struct woe_timer), 1);
-}
-
-struct woe_timer * woe_timer_init(struct woe_timer * me,
-  struct woe_server * server, int index) {
-  sigevent_t sev;
-  if (!me) return NULL;
-  me->server  = server;
-  me->index   = index;
-  me->set     = 0;
-  sev.sigev_notify= SIGEV_NONE;
-  
-  sev.sigev_notify = SIGEV_SIGNAL;
-  sev.sigev_signo  = SIGRTMIN + (index % (SIGRTMAX - SIGRTMIN - 1));
-  sev.sigev_value.sival_ptr = me;
-  
-  
-  if (timer_create(CLOCK_MONOTONIC, &sev, &me->timer) < 0) { 
-    LOG_ERROR("Could not create timer %d.", index);
-    return NULL;
-  }
-  LOG_NOTE("Timer %d initialized: %d.\n", me->index, me->timer);
-
-  return me;
-}
-
-struct woe_timer * woe_timer_new(struct woe_server * server, int index) {
-  struct woe_timer * me = woe_timer_alloc();
-  if (!me) return NULL;  
-  if (!woe_timer_init(me, server, index)) { 
-    free(me);
-    return NULL;
-  }
-  return me;
-}
-
-struct woe_timer * woe_timer_done(struct woe_timer * me) {
-  if (!me) return NULL;
-  if (me->timer)  timer_delete(me->timer);
-  me->timer     = NULL;
-  LOG_NOTE("Timer %d destroyed.\n", me->index);
-  me->index     = -1;
-  return me;
-}
-
-struct woe_timer * woe_timer_free(struct woe_timer * me) {
-  woe_timer_done(me);
-  free(me);  
-  return NULL;
-}
-
-struct timespec * timespec_init(struct timespec * tv, double sec) {
-  if (!tv) return NULL;
-  if (sec <= 0.0) {
-    tv->tv_sec  = 0;
-    tv->tv_nsec = 0;
-  } else {
-    tv->tv_sec  = floor(sec);
-    tv->tv_nsec = (sec - floor(sec)) * 1000000000;
-  }
-  return tv;
-} 
-
-double timespec_to_s(struct timespec * tv) {
-  return tv->tv_sec + ( tv->tv_nsec / 1000000000.0 );
-}
-
-int woe_timer_get(struct woe_timer * me, double * value, double * interval) {
-  int res;
-  if (!me) return -1;
-  struct itimerspec get;
-  /* timespec_init(&nv.it_value, value); */ 
-  res = timer_gettime(me->timer, &get);
-  if (res == 0) {
-    if (value)    (*value)    = timespec_to_s(&get.it_value);
-    if (interval) (*interval) = timespec_to_s(&get.it_interval);
-  }
-  return res;  
-}
-
-int woe_timer_set(struct woe_timer * me, double value, double interval) {
-   if (!me) return -1;
-   me->set = 1;
-   struct itimerspec nv, ov;
-   timespec_init(&nv.it_value, value);
-   timespec_init(&nv.it_interval, interval);
-   return timer_settime(me->timer, 0, &nv, &ov);
-}
-
-int woe_timer_passed(struct woe_timer * me) {
-  double value, interval;
-  if (!me) return 0;
-  if (!me->set) return 0;
-  if (woe_timer_get(me, &value, &interval) != 0) { 
-    LOG_ERROR("Trouble getting timer %d\n", me->index);
-    return 0;
-  }
-  return value <= 0;
-}
-
-int woe_timer_callback(struct woe_timer * me) {
-  mrb_state * mrb;
-  LOG_DEBUG("Timer passed: %d\n", me->index);
-  mrb = woe_server_get_mrb(me->server);  
-  if (mrb) {  
-    double value = 0.0, interval = 0.0;
-    woe_timer_get(me, &value, &interval); 
-    rh_run_toplevel(mrb, "woe_on_timer", "iff", me->index, value, interval);
-  }
-  return 0;
-}
-
-

+ 0 - 530
oldsrc/toruby.c

@@ -1,530 +0,0 @@
-/*
-* toruby.c helps expose functionality from C to Mruby for Eruta.
-* All functions are prefixed with tr_.
-* Look at the tr_*.c files.
-* */
-
-#define _XOPEN_SOURCE 700     
-
-#include "toruby.h"
-#include "tr_macro.h"
-#include "monolog.h"
-#include "rh.h"
-#include "state.h"
-#include "server.h"
-#include "libtelnet.h"
-#include "tr_file.h"
-
-#include <unistd.h>
-#include <signal.h>
-#include <mruby/hash.h>
-#include <mruby/class.h>
-#include <mruby/data.h>
-#include <mruby/array.h>
-
-/*
-#include "tr_macro.h"
-#include "tr_audio.h"
-#include "tr_graph.h"
-#include "tr_store.h"
-#include "tr_sprite.h"
-*/
-
-/* Documentation of mrb_get_args: 
- 
-  retrieve arguments from mrb_state.
-
-  mrb_get_args(mrb, format, ...)
-
-  returns number of arguments parsed.
-
-  format specifiers:
-
-   o: Object [mrb_value]
-   S: String [mrb_value]
-   A: Array [mrb_value]
-   H: Hash [mrb_value]
-   s: String [char*,int]
-   z: String [char*] nul terminated
-   a: Array [mrb_value*,mrb_int]
-   f: Float [mrb_float]
-   i: Integer [mrb_int]
-   b: Binary [int]
-   n: Symbol [mrb_sym]
-   &: Block [mrb_value]
-   *: rest argument [mrb_value*,int]
-   |: optional
- */
-
-
-
-
-/** Writes a NOTE message to the log. */
-static mrb_value tr_log(mrb_state * mrb, mrb_value self) {
-  (void) self; (void) mrb;
-  
-  mrb_value text    = mrb_nil_value();
-  mrb_get_args(mrb, "S", &text);
-  
-  LOG_NOTE("%s\n", RSTRING_PTR(text));
-  return self;
-}
-
-/** Writes a messageto a certain log level log. */
-static mrb_value tr_log_to(mrb_state * mrb, mrb_value self) {
-  (void) self; (void) mrb;
-  
-  mrb_value level   = mrb_nil_value();
-  mrb_value text    = mrb_nil_value();
-
-  mrb_get_args(mrb, "SS", &level, &text);
-  
-  LOG_LEVEL(RSTRING_PTR(level), "%s\n", RSTRING_PTR(text));
-  return self;
-}
-
-
-/** Cause a warning to be logged */
-static mrb_value tr_warn(mrb_state * mrb, mrb_value self) {
-  (void) self; (void) mrb;
-  
-  mrb_value text    = mrb_nil_value();
-  mrb_get_args(mrb, "S", &text);
-  LOG_WARNING("%s\n", RSTRING_PTR(text));
-  return self;
-}
-
-
-/** Enables a certain log level */
-static mrb_value tr_log_enable(mrb_state * mrb, mrb_value self) {
-  (void) self; (void) mrb;
-  
-  mrb_value text    = mrb_nil_value();
-  mrb_get_args(mrb, "S", &text);
-  monolog_enable_level(RSTRING_PTR(text));
-  return self;
-}
-
-/** Disables a certain log level */
-static mrb_value tr_log_disable(mrb_state * mrb, mrb_value self) {
-  (void) self; (void) mrb;
-
-  mrb_value text    = mrb_nil_value();
-  mrb_get_args(mrb, "S", &text);
-  monolog_disable_level(RSTRING_PTR(text));
-  return self;
-}
-
-
-/* Loads another script from the script directory. */
-static mrb_value tr_script(mrb_state * mrb, mrb_value self) {
-  int res; 
-  char * command;
-  
-  (void) self;
-  
-  mrb_value text        = mrb_nil_value();
-  mrb_get_args(mrb, "S", &text);
-  command               = mrb_str_to_cstr(mrb, text);
-  
-  res = rh_run_script(mrb, command);
-  return mrb_fixnum_value(res);
-}
-
-
-/* Sends data to a given client */
-static mrb_value tr_send_to_client(mrb_state * mrb, mrb_value self) {
-  int res; 
-  mrb_int client = -1;
-  char * data    = NULL;
-  int    size    = 0;
-  struct woe_server * srv = MRB_WOE_SERVER(mrb);
-  
-  (void) self;
-  mrb_get_args(mrb, "is", &client, &data, &size);
-  res = woe_server_send_to_client(srv, client, data, size);
-  
-  return mrb_fixnum_value(res);
-}
-
-/* Shuts down a given client */
-static mrb_value tr_server_done(mrb_state * mrb, mrb_value self) {
-  struct woe_server * srv = MRB_WOE_SERVER(mrb);
-  
-  (void) self;
-  woe_server_request_shutdown(srv);
-  return mrb_nil_value(); 
-}
-
-
-/* Disconnects a given client by id */
-static mrb_value tr_disconnect_client(mrb_state * mrb, mrb_value self) {
-  int res; 
-  mrb_int client;
-  struct woe_server * srv = MRB_WOE_SERVER(mrb);
-  
-  (void) self;
-  mrb_get_args(mrb, "i", &client);
-  res = woe_server_disconnect_id(srv, client);
-  
-  return mrb_fixnum_value(res);
-}
-
-
-/* Yeah, I know, but this reduces boilerplate. */
-#define WRAP_SERVER_BEGIN(NAME) \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {          \
-  int res;                                                        \
-  mrb_int client = -1;                                            \
-  struct woe_server * srv = MRB_WOE_SERVER(mrb);                  \
-  (void) self;                                                    \
-  
-
-#define WRAP_SERVER_TIMER_BEGIN(NAME) \
-static mrb_value NAME(mrb_state * mrb, mrb_value self) {          \
-  int res;                                                        \
-  mrb_int timer = -1;                                             \
-  struct woe_server * srv = MRB_WOE_SERVER(mrb);                  \
-  (void) self;                                                    \
-
-  
-#define WRAP_SERVER_END() \
-  return mrb_fixnum_value(res); \
-}
-    
-
-WRAP_SERVER_BEGIN(tr_server_iac) { 
-  mrb_int command = 0;  
-  mrb_get_args(mrb, "ii", &client, &command);
-  res = woe_server_iac(srv, client, command);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_negotiate) { 
-  mrb_int how = 0, option = 0;  
-  mrb_get_args(mrb, "iii", &client, &how, &option);
-  res = woe_server_negotiate(srv, client, how, option);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_begin_sb) { 
-  mrb_int telopt = 0;  
-  mrb_get_args(mrb, "ii", &client, &telopt);
-  res = woe_server_begin_sb(srv, client, telopt);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_finish_sb) { 
-  mrb_get_args(mrb, "i", &client);
-  res = woe_server_finish_sb(srv, client);
-} WRAP_SERVER_END()
-
-
-WRAP_SERVER_BEGIN(tr_server_subnegotiation) { 
-  mrb_int telopt = 0, size = 0;
-  char * data = NULL;  
-  mrb_get_args(mrb, "iis", &client, &telopt, &data, &size);
-  res = woe_server_subnegotiation(srv, client, telopt, data, size);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_begin_compress2) { 
-  mrb_get_args(mrb, "i", &client);
-  res = woe_server_begin_compress2(srv, client);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_puts) { 
-  char * fmt = NULL;  
-  mrb_get_args(mrb, "iz", &client, &fmt);
-  res = woe_server_printf(srv, client, fmt);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_raw_puts) { 
-  char * fmt = NULL;  
-  mrb_get_args(mrb, "iz", &client, &fmt);
-  res = woe_server_raw_printf(srv, client, fmt);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_begin_newenviron) { 
-  mrb_int type;
-  mrb_get_args(mrb, "ii", &client, &type);
-  res = woe_server_begin_newenviron(srv, client, type);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_newenviron_value) { 
-  mrb_int type;
-  char * value = NULL;  
-  mrb_get_args(mrb, "iiz", &client, &type, &value);
-  res = woe_server_newenviron_value(srv, client, type, value);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_finish_newenviron) { 
-  mrb_get_args(mrb, "i", &client);
-  res = woe_server_finish_newenviron(srv, client);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_ttype_send) { 
-  mrb_get_args(mrb, "i", &client);
-  res = woe_server_ttype_send(srv, client);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_ttype_is) { 
-  mrb_int type;
-  char * ttype = NULL;  
-  mrb_get_args(mrb, "iz", &client, &ttype);
-  res = woe_server_ttype_is(srv, client, ttype);
-} WRAP_SERVER_END()
-
-/*
-int woe_server_send_zmp(struct woe_server * srv, int client, int argc, const char ** argv);
-int woe_server_send_vzmpv(struct woe_server * srv, int client, va_list va);
-int woe_server_send_zmpv(struct woe_server * srv, int client, ...);
-*/
-WRAP_SERVER_BEGIN(tr_server_begin_zmp) { 
-  char * command = NULL;
-  mrb_get_args(mrb, "iz", &client, &command);
-  res = woe_server_begin_zmp(srv, client, command);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_zmp_arg) { 
-  char * arg = NULL;  
-  mrb_get_args(mrb, "iz", &client, &arg);
-  res = woe_server_zmp_arg(srv, client, arg);
-} WRAP_SERVER_END()
-
-WRAP_SERVER_BEGIN(tr_server_finish_zmp) { 
-  char * command;
-  mrb_get_args(mrb, "iz", &client, &command);
-  res = woe_server_finish_zmp(srv, client, command);
-} WRAP_SERVER_END()
-
-
-WRAP_SERVER_TIMER_BEGIN(tr_server_new_timer) { 
-  (void) timer; 
-  res = woe_server_make_new_timer_id(srv);
-} WRAP_SERVER_END()
-
-
-WRAP_SERVER_TIMER_BEGIN(tr_server_set_timer) { 
-  mrb_float value = 0.0, interval = 0.0;
-  mrb_get_args(mrb, "iff", &timer, &value, &interval);
-  res = woe_server_set_timer_value(srv, timer, value, interval);
-} WRAP_SERVER_END()
-
-static mrb_value tr_server_get_timer(mrb_state * mrb, mrb_value self) {
-  int res;                                                        
-  mrb_int timer = -1;                                             
-  struct woe_server * srv = MRB_WOE_SERVER(mrb);                  
-  (void) self;
-  double value = 0.0, interval = 0.0;
-  mrb_get_args(mrb, "i", &timer);
-  res = woe_server_get_timer_value(srv, timer, &value, &interval);
-  { 
-    mrb_value vals[3] = { mrb_fixnum_value(res), mrb_float_value(mrb, value), mrb_float_value(mrb, interval) };
-    return mrb_ary_new_from_values(mrb, 3, vals);
-  }
-}
-
-
-static mrb_value tr_crypt(mrb_state * mrb, mrb_value self) {
-  mrb_value res;
-  char * pass = NULL;
-  char * salt = NULL;
-  char * hash = NULL;
-  mrb_get_args(mrb, "zz", &pass, &salt);
-  hash = crypt(pass, salt);
-  return mrb_str_new(mrb, hash, 13);
-}
-                            
-
-
-/* Initializes the functionality that Eruta exposes to Ruby. */
-int tr_init(mrb_state * mrb) {
-  // luaL_dostring(lua, "print 'Hello!' ");
-  struct RClass *woe;
-  struct RClass *srv;
-  struct RClass *krn;
-  struct RClass *tel;
-  struct RClass *sig;
-  struct RClass *fil;
-  
-  tr_init_file(mrb);
- 
-  woe = mrb_define_module(mrb, "Woe");
-  srv = mrb_define_module_under(mrb, woe, "Server"); 
-  tel = mrb_define_module(mrb, "Telnet");
-  sig = mrb_define_module(mrb, "Signal");
-  krn = mrb_module_get(mrb, "Kernel");
-  if(!krn) return -1;
-  
-  TR_CLASS_METHOD_ARGC(mrb  , woe, "crypt"  , tr_crypt, 2);
-  TR_CLASS_METHOD_NOARG(mrb , woe,  "quit"  , tr_server_done);
-  TR_CLASS_METHOD_NOARG(mrb , srv, "quit"  , tr_server_done);
-  TR_CLASS_METHOD_ARGC(mrb  , srv, "send_to_client"  , tr_send_to_client, 2);
-  TR_CLASS_METHOD_NOARG(mrb , srv, "disconnect"  , tr_disconnect_client);
-  
-  TR_CLASS_METHOD_ARGC(mrb, srv, "iac"  , tr_server_iac, 2);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "negotiate"      , tr_server_negotiate     , 3);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "begin_sb"       , tr_server_begin_sb      , 2);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "finish_sb"      , tr_server_finish_sb     , 1);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "subnegotiation" , tr_server_subnegotiation, 3);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "begin_compress2", tr_server_begin_compress2, 2);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "puts"           , tr_server_puts, 2);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "raw_puts"       , tr_server_raw_puts, 2);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "begin_newenviron" , tr_server_begin_newenviron, 2);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "newenviron_value" , tr_server_newenviron_value, 3);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "finish_newenviron", tr_server_finish_newenviron, 1);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "ttype_send"  , tr_server_ttype_send, 1);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "ttype_is"    , tr_server_ttype_is, 2);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "begin_zmp"  , tr_server_finish_zmp, 2);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "zmp_arg"  , tr_server_finish_zmp, 2);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "finish_zmp"  , tr_server_finish_zmp, 2);
-
-  TR_CLASS_METHOD_NOARG(mrb, srv, "new_timer"  , tr_server_new_timer);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "set_timer"  , tr_server_set_timer, 3);
-  TR_CLASS_METHOD_ARGC(mrb, srv, "get_timer"  , tr_server_get_timer, 1);
-  
-  /* Telnet constants, commands, etc. */
-  
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_IAC);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_DONT);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_DO);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_WILL);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_WONT);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_SB);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_GA);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_EL);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_EC);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_AYT);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_AO);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_IP);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_BREAK);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_DM);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_NOP);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_SE);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_EOR);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_ABORT);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_SUSP);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_EOF);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_BINARY);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_ECHO);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_RCP);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_SGA);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NAMS);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_STATUS);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_TM);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_RCTE);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NAOL);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NAOP);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NAOCRD);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NAOHTS);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NAOHTD);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NAOFFD);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NAOVTS);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NAOVTD);
-  
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NAOLFD);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_XASCII);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_LOGOUT);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_BM);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_DET);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_SUPDUP);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_SUPDUPOUTPUT);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_SNDLOC);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_TTYPE);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_EOR);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_3270REGIME);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_X3PAD);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NAWS);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_TSPEED);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_LFLOW);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_LINEMODE);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_XDISPLOC);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_ENVIRON);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_AUTHENTICATION);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_ENCRYPT);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_NEW_ENVIRON);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_MSSP);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_COMPRESS);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_COMPRESS2);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_ZMP);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_EXOPL);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TELOPT_MCCP2);
-
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TTYPE_IS);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_TTYPE_SEND);
-
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_ENVIRON_IS);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_ENVIRON_SEND);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_ENVIRON_INFO);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_ENVIRON_VAR);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_ENVIRON_VALUE);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_ENVIRON_ESC);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_ENVIRON_USERVAR);
-
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_MSSP_VAL);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_MSSP_VAR);
-
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_EOK);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_EBADVAL);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_ENOMEM);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_EOVERFLOW);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_EPROTOCOL);
-  TR_CONST_INT_VALUE(mrb, tel, TELNET_ECOMPRESS);
-
-  TR_CONST_INT_VALUE(mrb, sig, SIGHUP);
-  TR_CONST_INT_VALUE(mrb, sig, SIGINT);
-  TR_CONST_INT_VALUE(mrb, sig, SIGQUIT);
-  TR_CONST_INT_VALUE(mrb, sig, SIGILL);
-  TR_CONST_INT_VALUE(mrb, sig, SIGABRT);
-  TR_CONST_INT_VALUE(mrb, sig, SIGFPE);
-  TR_CONST_INT_VALUE(mrb, sig, SIGKILL);
-  TR_CONST_INT_VALUE(mrb, sig, SIGSEGV);
-  TR_CONST_INT_VALUE(mrb, sig, SIGPIPE);
-  TR_CONST_INT_VALUE(mrb, sig, SIGALRM);
-  TR_CONST_INT_VALUE(mrb, sig, SIGTERM);
-  TR_CONST_INT_VALUE(mrb, sig, SIGUSR1);
-  TR_CONST_INT_VALUE(mrb, sig, SIGUSR2);
-  TR_CONST_INT_VALUE(mrb, sig, SIGCHLD);
-  TR_CONST_INT_VALUE(mrb, sig, SIGCONT);
-  TR_CONST_INT_VALUE(mrb, sig, SIGSTOP);
-  TR_CONST_INT_VALUE(mrb, sig, SIGTSTP);
-  TR_CONST_INT_VALUE(mrb, sig, SIGTTIN);
-  TR_CONST_INT_VALUE(mrb, sig, SIGTTOU);
-  TR_CONST_INT_VALUE(mrb, sig, SIGBUS);
-  TR_CONST_INT_VALUE(mrb, sig, SIGPOLL);
-  TR_CONST_INT_VALUE(mrb, sig, SIGPROF);
-  TR_CONST_INT_VALUE(mrb, sig, SIGSYS);
-  TR_CONST_INT_VALUE(mrb, sig, SIGTRAP);
-  TR_CONST_INT_VALUE(mrb, sig, SIGURG);
-  TR_CONST_INT_VALUE(mrb, sig, SIGVTALRM);
-  TR_CONST_INT_VALUE(mrb, sig, SIGXCPU);  
-  TR_CONST_INT_VALUE(mrb, sig, SIGXFSZ);
-  TR_CONST_INT_VALUE(mrb, sig, SIGIOT);
-  TR_CONST_INT_VALUE(mrb, sig, SIGSTKFLT);
-  TR_CONST_INT_VALUE(mrb, sig, SIGIO);
-  TR_CONST_INT_VALUE(mrb, sig, SIGCLD);
-  TR_CONST_INT_VALUE(mrb, sig, SIGPWR);
-  TR_CONST_INT_VALUE(mrb, sig, SIGWINCH);
-  TR_CONST_INT_VALUE(mrb, sig, SIGUNUSED);
-
-  
-  
-  TR_METHOD_ARGC(mrb, krn, "woe_warn"     , tr_warn   , 1);
-  TR_METHOD_ARGC(mrb, krn, "woe_warning"  , tr_warn   , 1);
-  TR_METHOD_ARGC(mrb, krn, "woe_log"      , tr_log    , 1);
-  TR_METHOD_ARGC(mrb, krn, "woe_log_to"   , tr_log_to , 2);
-  TR_METHOD_ARGC(mrb, krn, "log_enable"   , tr_log_disable , 1);
-  TR_METHOD_ARGC(mrb, krn, "log_disable"  , tr_log_enable  , 1);
-  TR_METHOD_ARGC(mrb, krn, "script"       , tr_script , 1);
-
-   
-  // must restore gc area here ????
-  mrb_gc_arena_restore(mrb, 0);
-  
-  return 0;
-}
-
-
-
-
-
-
-
-

+ 0 - 396
oldsrc/tr_file.c

@@ -1,396 +0,0 @@
-
-#include <errno.h>
-#include <stdio.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <dirent.h>
-
-
-#include "esh.h"
-#include "toruby.h"
-#include "tr_macro.h"
-#include "monolog.h"
-#include "rh.h"
-#include "state.h"
-#include "server.h"
-#include "tr_file.h"
-
-
-struct tr_file {
-  FILE              * file;
-  struct woe_config * cfg;
-};
-
-typedef struct tr_file tr_file;
-
-static void file_close(tr_file * file) {
-  if (!file) return;
-  if (!file->file) return;
-  fclose(file->file);
-  file->file = NULL;
-}
-
-static void tr_file_free(mrb_state *mrb, void *ptr) {
-  struct tr_file * file = ptr;
-  file_close(file);
-  mrb_free(mrb, file);
-}
-
-static FILE * file_fopen(mrb_state * mrb, char * filename, char * mode) 
-{
-  struct woe_config * cfg;
-  struct woesb buf = { 0 };
-  FILE * me = NULL;
-  if (!mrb) return NULL;
-  cfg = MRB_WOE_CONFIG(mrb);
-  if (!cfg) return NULL;
-  
-  if (!woesb_new_join(&buf, cfg->data_dir, "/var/", filename, NULL)) {
-    LOG_ERROR("Cannot allocate space for file name.\n");
-    return NULL;
-  }
-  
-  if (strstr(buf.text, "..")) {
-    mrb_free(mrb, me);
-    woesb_free(&buf);
-    LOG_ERROR("Path may not contain '..' \n");
-    return NULL;
-  }
-
-  me = fopen(buf.text, mode);
-  if (!me) {
-    LOG_ERROR("Cannot open file %s.\n", filename);
-    woesb_free(&buf);
-    return NULL;
-  }
-  
-  woesb_free(&buf);
-  return me;
-}
-
-
-static tr_file * file_open(mrb_state * mrb, char * filename, char * mode) 
-{
-  struct woe_config * cfg;
-  tr_file * me = NULL;
-  if (!mrb) return NULL;
-  cfg = MRB_WOE_CONFIG(mrb);
-  if (!cfg) return NULL;
-  
-  me  = mrb_malloc(mrb, sizeof(struct tr_file));
-  if (!me) return NULL;
-  me->file = file_fopen(mrb, filename, mode);
-  if (!me->file) {
-    mrb_free(mrb, me);
-    return NULL;
-  }
-  me->cfg = cfg; 
-  return me;
-}
-
-int woe_mkdir(struct woe_config * cfg, char * filename) {
-  int res;
-  DIR * dir;
-  
-  struct woesb buf = { 0 };
-  if (!cfg) return -3;
- 
-  if (!woesb_new_join(&buf, cfg->data_dir, "/var/", filename, NULL)) {
-    LOG_ERROR("Cannot allocate space for file name.\n");
-    return -1;
-  }
-  
-  if (strstr(buf.text, "..")) {
-    woesb_free(&buf);
-    LOG_ERROR("Path may not contain '..' \n");
-    return -2;
-  }
-  
-  dir = opendir(buf.text);
-  if (dir) {
-    LOG_DEBUG("Dir %s already exists\n");
-    /* Directory already exists */
-    closedir(dir);
-    woesb_free(&buf);
-    return 0; 
-  }
-  
-  errno = 0;
-  res = mkdir(buf.text , 0770);
-
-  if (res < 0) {
-    LOG_ERROR("Cannot make dir %s.\n", filename);
-    woesb_free(&buf);
-    return errno;
-  }
-  
-  woesb_free(&buf);
-  return 0;
-}
-
-int woe_link(struct woe_config * cfg, char * old_filename, char * new_filename) {
-  int res;
-  
-  struct woesb old_buf = { 0 };
-  struct woesb new_buf = { 0 };
-
-  if (!cfg) return -3;
- 
-  if (!woesb_new_join(&old_buf, cfg->data_dir, "/var/", old_filename, NULL)) {
-    LOG_ERROR("Cannot allocate space for file name.\n");
-    return -1;
-  }
-
-  if (!woesb_new_join(&new_buf, cfg->data_dir, "/var/", new_filename, NULL)) {
-    woesb_free(&old_buf);
-    LOG_ERROR("Cannot allocate space for file name.\n");
-    return -1;
-  }
-
-  
-  if (strstr(old_buf.text, "..") || strstr(new_buf.text, "..")) {
-    woesb_free(&new_buf);
-    woesb_free(&old_buf);    
-    LOG_ERROR("Path may not contain '..' \n");
-    return -2;
-  }
-  
-  errno = 0;
-  res = link(old_buf.text, new_buf.text);
-
-  if (res < 0) {
-    LOG_ERROR("Cannot link %s to %s.\n", old_filename, new_filename);
-    woesb_free(&new_buf);
-    woesb_free(&old_buf);
-    return errno;
-  }
-  
-  woesb_free(&new_buf);
-  woesb_free(&old_buf);
-  return 0;
-}
-
-
-struct mrb_data_type tr_file_type = { "File", tr_file_free };
-
-static mrb_value tr_file_wrap(mrb_state *mrb, struct RClass *tc, tr_file * file) {
-  return mrb_obj_value(Data_Wrap_Struct(mrb, tc, &tr_file_type, file));
-}
-
-tr_file * tr_file_unwrap(mrb_state *mrb, mrb_value val) {
-  return DATA_CHECK_GET_PTR(mrb, val, &tr_file_type, tr_file);
-}
-
-static mrb_value tr_file_open(mrb_state * mrb, mrb_value self) {
-  int res; 
-  char * name, * mode;
-  tr_file * file; 
-  
-  mrb_get_args(mrb, "zz", &name, &mode);
-  file = file_open(mrb, name, mode);
-  
-  if (!file) return mrb_nil_value();
-  
-  return tr_file_wrap(mrb, mrb_class_get(mrb, "File"), file);
-     
-}    
-  
-static mrb_value tr_file_close(mrb_state * mrb, mrb_value self) {
-  file_close(tr_file_unwrap(mrb, self));
-  return mrb_nil_value();
-}
-
-static mrb_value tr_file_write(mrb_state * mrb, mrb_value self) { 
-  mrb_int res, size;
-  tr_file * file; 
-  char * buf;
-  file = tr_file_unwrap(mrb, self);
-  mrb_get_args(mrb, "s", &buf, &size);
-  res = fwrite(buf, size, 1, file->file);
-  return mrb_fixnum_value(res);
-}
-
-static mrb_value tr_file_read(mrb_state * mrb, mrb_value self) {
-  mrb_int res, size;
-  tr_file * file;
-  char * mem; 
-  mrb_value buf;
-  
-  
-  file = tr_file_unwrap(mrb, self);
-  mrb_get_args(mrb, "i", &size);
-  mem = calloc(size, 1);
-  res = fread(mem, 1, size, file->file);
-  if (res > 0) {
-    buf = mrb_str_new(mrb, mem, size);
-    free(mem);
-    return buf;
-  } 
-  
-  if (res == 0) {
-    free(mem);
-    return mrb_nil_value();
-  }
-  
-  // if (res < 0)
-  free(mem);
-  LOG_ERROR("Failed to read from file.\n");
-  return mrb_nil_value();
-}   
-
-
-static mrb_value tr_file_readall(mrb_state * mrb, mrb_value self) {
-  #define READALL_BUFSIZE 1024
-  mrb_int res = 0, size = 0;
-  FILE * file;
-  char * mem = NULL, * aid = NULL; 
-  char * filename = NULL;
-  mrb_value buf;
-  (void) self;
-  
-  
-  mrb_get_args(mrb, "z", &filename);
-  if (!filename) {
-      return mrb_nil_value();
-  }
-  
-  file = file_fopen(mrb, filename, "rb");
-  
-  if (!file) { 
-    return mrb_nil_value();
-  }
-  
-  while (!feof(file)) {
-    size += READALL_BUFSIZE;
-    aid   = realloc(mem, size);
-    if (!aid) { 
-      buf = mrb_nil_value();
-      goto done;
-    }
-    mem = aid;
-    res   = fread(mem + size - READALL_BUFSIZE, 1, READALL_BUFSIZE, file);
-    
-    if (res < READALL_BUFSIZE) {
-      size = size - READALL_BUFSIZE + res;
-      break;
-    }
-  }
-  buf = mrb_str_new(mrb, mem, size);
-  
-  done:  
-    free(mem);
-    fclose(file);
-    return buf;
-}   
- 
-
-static mrb_value tr_file_puts(mrb_state * mrb, mrb_value self) { 
-  mrb_int res, size;
-  tr_file * file; 
-  char * buf;
-  file = tr_file_unwrap(mrb, self);
-  mrb_get_args(mrb, "z", &buf);
-  res = fputs(buf, file->file);
-  return mrb_fixnum_value(res);
-}
-
-
-static mrb_value tr_file_gets(mrb_state * mrb, mrb_value self) {
-  mrb_int size;
-  tr_file * file; 
-  char * buf;
-  char * check;
-  mrb_value res;
-  
-  file = tr_file_unwrap(mrb, self);
-  mrb_get_args(mrb, "i", &size);
-  buf = calloc(size, 1);
-  if (!buf) {
-    return mrb_nil_value();
-  }
-  
-  check = fgets(buf, size, file->file);
-  if (check) {
-    res = mrb_str_new_cstr(mrb, buf);
-    free(buf); 
-    return res;
-  } 
-  
-  free(buf);
-  return mrb_nil_value();
-}
-
-
-static mrb_value tr_file_putc(mrb_state * mrb, mrb_value self) { 
-  mrb_int res, chara = 0;
-  tr_file * file; 
-  file = tr_file_unwrap(mrb, self);
-  mrb_get_args(mrb, "i", &chara);
-  res = fputc(chara, file->file);
-  return mrb_fixnum_value(res);
-}
-
-
-static mrb_value tr_file_getc(mrb_state * mrb, mrb_value self) {
-  mrb_int res;
-  tr_file * file; 
-  file = tr_file_unwrap(mrb, self);
-  res = fgetc(file->file);
-  return mrb_fixnum_value(res);
-} 
- 
-static mrb_value tr_file_eof(mrb_state * mrb, mrb_value self) {
-  tr_file * file; 
-  file = tr_file_unwrap(mrb, self);  
-  return rh_bool_value(feof(file->file));
-}
-
-static mrb_value tr_dir_mkdir(mrb_state * mrb, mrb_value self) {
-  tr_file * file; 
-  char * name;
-  struct woe_config * cfg;
-  cfg = MRB_WOE_CONFIG(mrb);
-  file = tr_file_unwrap(mrb, self);
-  mrb_get_args(mrb, "z", &name);
-  return mrb_fixnum_value(woe_mkdir(cfg, name));
-}
-
-static mrb_value tr_file_link(mrb_state * mrb, mrb_value self) {
-  tr_file * file; 
-  char * old_name, * new_name;
-  struct woe_config * cfg;
-  cfg = MRB_WOE_CONFIG(mrb);
-  file = tr_file_unwrap(mrb, self);
-  mrb_get_args(mrb, "zz", &old_name, &new_name);
-  return mrb_fixnum_value(woe_link(cfg, old_name, new_name));
-}
-
-
-int tr_init_file(mrb_state * mrb) {
-  struct RClass *krn;
-  struct RClass *dir;
-  struct RClass *fil;
-  
-  fil = mrb_define_class(mrb, "File"    , mrb_class_get(mrb, "Object"));
-  dir = mrb_define_class(mrb, "Dir"     , mrb_class_get(mrb, "Object"));
-
-  TR_CLASS_METHOD_ARGC(mrb, fil, "read" , tr_file_readall, 1);
-  TR_CLASS_METHOD_ARGC(mrb, fil, "open" , tr_file_open, 2);
-  TR_CLASS_METHOD_ARGC(mrb, fil, "link" , tr_file_link, 2);
-  TR_CLASS_METHOD_ARGC(mrb, dir, "mkdir", tr_dir_mkdir, 1);
-
-
-  TR_METHOD_NOARG(mrb, fil, "close"     , tr_file_close);
-  TR_METHOD_NOARG(mrb, fil, "eof?"       , tr_file_eof);
-  TR_METHOD_ARGC(mrb, fil, "read"       , tr_file_read, 1);
-  TR_METHOD_ARGC(mrb, fil, "write"      , tr_file_read, 2);
-  TR_METHOD_ARGC(mrb, fil, "gets"       , tr_file_gets, 1);
-  TR_METHOD_ARGC(mrb, fil, "puts"       , tr_file_puts, 1);
-  TR_METHOD_ARGC(mrb, fil, "putc"       , tr_file_putc, 1);
-  TR_METHOD_ARGC(mrb, fil, "getc"       , tr_file_getc, 1);
-  
-  return 0;
-}