1 module minimono;
2 /*
3 	Source file is	MiniMono, MiniM Embedded Edition	(C) Eugene Karataev
4 	Header for the MiniMono DLL
5 
6 	http://www.minimdb.com
7 	support@minimdb.com
8 	
9 	Implementation for D - Mohow Gennady MGW
10 	https://github.com/MGWL/QtE5 -- Qt-5, Forth, MiniMono, MiniM for D
11 	mgw@yandex.ru
12 */
13 import core.runtime;	// Load DLL for Windows
14 import zdll;			//C		#include "zdll.h"
15 
16 version(linux) {   
17     import core.sys.posix.dlfcn;  // dlopen() и dlsym()
18 	extern (C) {
19 		void* rt_loadLibrary(const char* name) { return dlopen(name, RTLD_GLOBAL || RTLD_LAZY);   }
20 		void* GetProcAddress(void* hLib, string nameFun)  { return dlsym(hLib, nameFun.ptr);     }
21 		bool  FreeLibrary(void* hModule) { return dlclose(hModule) == 0 ? false : true;  }
22 	}
23 }
24 version(Windows) {
25 	import core.sys.windows.windows;  // GetProcAddress for Windows
26 }
27 version (OSX) {
28     private import core.sys.posix.dlfcn: dlclose, dlopen, dlsym, RTLD_GLOBAL, RTLD_LAZY;
29         // On Linux these functions aren't defined in core.runtime, here and it was necessary to add.
30         // It is strange why they aren't present there...
31         // Probably they in the main Windows twist.
32     private extern (C) void* rt_loadLibrary(const char* name) { return dlopen(name, RTLD_GLOBAL || RTLD_LAZY);  }
33     private void* GetProcAddress(void* hLib, const char* nameFun) {  return dlsym(hLib, nameFun);    }
34         bool  FreeLibrary(void* hModule) { return dlclose(hModule) == 0 ? false : true;  }
35 }
36 
37 extern (Windows) {
38 	alias  int function(ZDLLCB *cbfunc, MINIM_STR **param_pairs, int param_pairs_count)		dlldevuse_t;
39 	alias  int function(ZDLLCB *cbfunc, int len, int timeout, MINIM_STR *result)			dlldevreadstr_t;
40 	alias  int function(ZDLLCB *cbfunc, int timeout, int *result)							dlldevreadchar_t;
41 	alias  int function(ZDLLCB *cbfunc, MINIM_STR *str)										dlldevwritestr_t;
42 	alias  int function(ZDLLCB *cbfunc, int _char)											dlldevwritechar_t;
43 	alias  int function(ZDLLCB *cbfunc)														dlldevwritenl_t;
44 	alias  int function(ZDLLCB *cbfunc)														dlldevwriteff_t;
45 	alias  int function(ZDLLCB *cbfunc, int tabcount)										dlldevwritetab_t;
46 	alias  int function(ZDLLCB *cbfunc, int *result)										dlldevgetx_t;
47 	alias  int function(ZDLLCB *cbfunc, int value)											dlldevsetx_t;
48 	alias  int function(ZDLLCB *cbfunc, MINIM_STR *result)									dlldevgetkey_t;
49 	alias  int function(ZDLLCB *cbfunc, MINIM_STR *value)									dlldevsetkey_t;
50 	alias  int function(ZDLLCB *cbfunc, int *result)										dlldevzeof_t;
51 }
52 struct tagMINIMONOVM
53 {
54  align (1):
55 	immutable(char)*	DataFile;	// file of the database
56 	int ReadOnly;					// database in read only mode or write enabled
57 	int JournalingEnabled;			// is database journaled or not, journaling is required for transactions
58 	int LockAreaSize;				// size of lock table in megabytes
59 	int RoutineCacheSize;			// size of routine cache in megabytes
60 	int DeviceTableSize;			// number of devices can be used
61 	int DeviceNameSize;				// length of device name in bytes
62 	int DBCacheSize;				// size of global cache in megabytes
63 	int NullSubscripts;				// allow (1) or not (0) null subscripts
64 	int TransactLevelLimit;			// number of transaction levels allowed
65 	int TrapOnEof;					// raise error on end of file or not
66 	int FrameCount;					// number of stack frames
67 	int JournalCache;				// size of journal cache in megabytes;
68 	immutable(char)* LocaleFileName;// file name with MiniM locale definition or null
69 	int ProcessStorage;				// size of area for local variables in megabytes
70 	// Functions to call virtual machine
71 	ZDLLCB* cbfunc;					// This pointer must be assigned by CreateMiniMonoVM function
72 	dlldevuse_t 					Use;
73 	dlldevreadstr_t 				ReadStr;
74 	dlldevreadchar_t 				ReadChar;
75 	dlldevwritestr_t 				WriteStr;
76 	dlldevwritechar_t 				WriteChar;
77 	dlldevwritenl_t 				WriteNL;
78 	dlldevwriteff_t 				WriteFF;
79 	dlldevwritetab_t 				WriteTab;
80 	dlldevgetx_t 					GetX;
81 	dlldevgetx_t 					GetY;
82 	dlldevsetx_t 					SetX;
83 	dlldevsetx_t 					SetY;
84 	dlldevgetkey_t 					GetKEY;
85 	dlldevsetkey_t 					SetKEY;
86 	dlldevzeof_t 					GetZEOF;
87 	dlldevgetkey_t 					GetZA;
88 	dlldevgetkey_t 					GetZB;
89 	void* myAdr;
90 }
91 alias tagMINIMONOVM MINIMONOVM;
92 
93 const	MINIMONO_SUCCESS 		= 0; 
94 const	MINIMONO_CREATED 		= 1;
95 const	MINIMONO_CREATEFAILED 	= 2;
96 
97 alias 	void*			HMNMConnect;
98 
99 extern (Windows) {
100 	// -------------------- MiniMono ------------------------
101 	alias pure @nogc nothrow int 	function(MINIMONOVM *init)  								createminimonovm_t;
102 	alias pure @nogc nothrow void	function()  			  									freeminimonovm_t;
103 	alias pure @nogc nothrow void	function(MINIMONOVM *init)  								getdefaultsettingsvm_t;
104 	alias pure @nogc nothrow void	function(int set_break)										ctrlbreakvm_t;
105 
106 	alias pure @nogc nothrow int  	function(MINIM_STR *List, int pos, MINIM_STR *Element)		mnmlistget_t;
107 	alias pure @nogc nothrow int  	function(MINIM_STR *List, int pos, MINIM_STR *Element)		mnmlistset_t;
108 	alias pure @nogc nothrow int  	function(MINIM_STR *List)									mnmlistlength_t;
109 	alias pure @nogc nothrow int 	function(MINIM_STR *Source, MINIM_STR *Target)				mnmtext_t;
110 	// -------------------- MiniMsc ------------------------
111 	alias pure @nogc nothrow int	function(HMNMConnect pConnect)								MNMConnectClose_t;
112 	alias pure @nogc nothrow int	function(HMNMConnect pConnect)								MNMConnectOpen_t;
113 	alias pure @nogc nothrow HMNMConnect	function(char* server, int port, char* database)	MNMCreateConnect_t;
114 	alias pure @nogc nothrow void	function(HMNMConnect pConnect)								MNMDestroyConnect_t;
115 	alias pure @nogc nothrow int	function(HMNMConnect pConnect, MINIM_STR* Commands)			MNMExecute_t;
116 	alias pure @nogc nothrow void	function(HMNMConnect pConnect, MINIM_STR* pError)			MNMGetLastError_t;
117 	alias pure @nogc nothrow int	function(HMNMConnect pConnect, MINIM_STR* VarName)			MNMKill_t;
118 	alias pure @nogc nothrow int	function(HMNMConnect pConnect, MINIM_STR* Expression, 		MINIM_STR* Result)	MNMRead_t;
119 	alias pure @nogc nothrow int	function(HMNMConnect pConnect, MINIM_STR* VarName, MINIM_STR* VarValue)	MNMWrite_t;
120 	alias pure @nogc nothrow void	function(HMNMConnect pConnect, void* pProc)					MNMSetOutput_t;
121 	alias pure @nogc nothrow int	function(HMNMConnect pConnect, MINIM_STR* Commands)			MNMExecuteOutput_t;
122 	alias pure @nogc nothrow void	function(HMNMConnect pConnect, void* pProc)					MNMSetCallback_t;
123 	
124 }
125 
126 public createminimonovm_t			CreateMiniMonoVM;
127 public getdefaultsettingsvm_t		GetDefaultSettingsVM;
128 public freeminimonovm_t				FreeMiniMonoVM;
129 
130 public mnmtext_t					MNMText;
131 public mnmlistlength_t				MNMListLength;
132 public mnmlistset_t					MNMListSet;
133 public mnmlistget_t					MNMListGet;
134 
135 // -------------------- MiniMsc ------------------------
136 public MNMConnectClose_t			MNMConnectClose;
137 public MNMConnectOpen_t				MNMConnectOpen;
138 public MNMCreateConnect_t			MNMCreateConnect;
139 public MNMDestroyConnect_t			MNMDestroyConnect;
140 public MNMExecute_t					MNMExecute;
141 public MNMGetLastError_t			MNMGetLastError;
142 public MNMKill_t					MNMKill;
143 public MNMRead_t					MNMRead;
144 public MNMWrite_t					MNMWrite;
145 public MNMSetOutput_t				MNMSetOutput;
146 public MNMSetCallback_t				MNMSetCallback;
147 public MNMExecuteOutput_t			MNMExecuteOutput;
148 
149 version(Windows) {
150 	auto libMiniMono = "minimono.dll";
151 	auto libMiniMsc  = "minimsc.dll";
152 }
153 version(linux) {   
154 	auto libMiniMono = "libminimono.so";
155 	auto libMiniMsc  = "libminimsc.so";
156 }
157 version (OSX) {
158         auto libMiniMono = "libminimono.dylib";
159         auto libMiniMsc  = "libminimsc.dylib";
160 }
161 
162 private static void* hMono;
163 private static void* hMsc;
164 
165 public int loadMiniMonoDll(string lib) {
166 	hMono = Runtime.loadLibrary(lib);
167 		if(!hMono) return MINIMONO_CREATEFAILED;
168 	
169 	CreateMiniMonoVM = cast(createminimonovm_t)GetProcAddress(hMono, "CreateMiniMonoVM");
170 		if(!CreateMiniMonoVM) return MINIMONO_CREATEFAILED;
171 	GetDefaultSettingsVM = cast(getdefaultsettingsvm_t)GetProcAddress(hMono, "GetDefaultSettingsVM");
172 		if(!GetDefaultSettingsVM) return MINIMONO_CREATEFAILED;
173 	FreeMiniMonoVM = cast(freeminimonovm_t)GetProcAddress(hMono, "FreeMiniMonoVM");
174 		if(!FreeMiniMonoVM) return MINIMONO_CREATEFAILED;
175 	// ---- work of list --------------------
176 	MNMListLength = cast(mnmlistlength_t)GetProcAddress(hMono, "MNMListLength");
177 		if(!MNMListLength) return MINIMONO_CREATEFAILED;
178 	MNMText = cast(mnmtext_t)GetProcAddress(hMono, "MNMText");
179 		if(!MNMText) return MINIMONO_CREATEFAILED;
180 	MNMListSet = cast(mnmlistset_t)GetProcAddress(hMono, "MNMListSet");
181 		if(!MNMListSet) return MINIMONO_CREATEFAILED;
182 	MNMListGet = cast(mnmlistget_t)GetProcAddress(hMono, "MNMListGet");
183 		if(!MNMListGet) return MINIMONO_CREATEFAILED;
184 	return MINIMONO_SUCCESS;
185 }
186 public int freeMiniMonoDll() {
187 	return FreeLibrary( hMono );
188 }
189 public int loadMiniMscDll(string lib) {
190 	hMsc = Runtime.loadLibrary(lib);
191 		if(!hMsc) return MINIMONO_CREATEFAILED;
192 	/* 1) disconnect from MiniM server */
193 	MNMConnectClose = cast(MNMConnectClose_t)GetProcAddress(hMsc, "MNMConnectClose");
194 		if(!MNMConnectClose) return MINIMONO_CREATEFAILED;
195 	/* connect to MiniM server */
196 	MNMConnectOpen = cast(MNMConnectOpen_t)GetProcAddress(hMsc, "MNMConnectOpen");
197 		if(!MNMConnectOpen) return MINIMONO_CREATEFAILED;
198 	/* создать связь с Minim */
199 	MNMCreateConnect = cast(MNMCreateConnect_t)GetProcAddress(hMsc, "MNMCreateConnect");
200 		if(!MNMCreateConnect) return MINIMONO_CREATEFAILED;
201 	/* destroy connect object */
202 	MNMDestroyConnect = cast(MNMDestroyConnect_t)GetProcAddress(hMsc, "MNMDestroyConnect");
203 		if(!MNMDestroyConnect) return MINIMONO_CREATEFAILED;
204 	/* execute commands */
205 	MNMExecute = cast(MNMExecute_t)GetProcAddress(hMsc, "MNMExecute");
206 		if(!MNMExecute) return MINIMONO_CREATEFAILED;
207 	/* get last error description */
208 	MNMGetLastError = cast(MNMGetLastError_t)GetProcAddress(hMsc, "MNMGetLastError");
209 		if(!MNMGetLastError) return MINIMONO_CREATEFAILED;
210 	/* kill variable */
211 	MNMKill = cast(MNMKill_t)GetProcAddress(hMsc, "MNMKill");
212 		if(!MNMKill) return MINIMONO_CREATEFAILED;
213 	/* read M expression */
214 	MNMRead = cast(MNMRead_t)GetProcAddress(hMsc, "MNMRead");
215 		if(!MNMRead) return MINIMONO_CREATEFAILED;
216 	/* write M variable */
217 	MNMWrite = cast(MNMWrite_t)GetProcAddress(hMsc, "MNMWrite");
218 		if(!MNMWrite) return MINIMONO_CREATEFAILED;
219 	/* set callback for ExecuteOutpet */
220 	MNMSetOutput = cast(MNMSetOutput_t)GetProcAddress(hMsc, "MNMSetOutput");
221 		if(!MNMSetOutput) return MINIMONO_CREATEFAILED;
222 	MNMSetCallback = cast(MNMSetCallback_t)GetProcAddress(hMsc, "MNMSetCallback");
223 		if(!MNMSetCallback) return MINIMONO_CREATEFAILED;
224 	MNMExecuteOutput = cast(MNMExecuteOutput_t)GetProcAddress(hMsc, "MNMExecuteOutput");
225 		if(!MNMExecuteOutput) return MINIMONO_CREATEFAILED;
226 	return MINIMONO_SUCCESS;
227 }
228