OpenCBM
WINDOWS/archlib.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version
5  * 2 of the License, or (at your option) any later version.
6  *
7  * Copyright 1999-2001 Michael Klein <michael(dot)klein(at)puffin(dot)lb(dot)shuttle(dot)de>
8  * Copyright 2001-2005,2007,2008 Spiro Trikaliotis
9  *
10 */
11 
20 #include <windows.h>
21 #include <stdlib.h>
22 
24 #define DBG_USERMODE
25 
27 #define DBG_DLL
28 
30 #define DBG_PROGNAME "OPENCBM.DLL"
31 
33 #define DBG_IS_DEBUG_C
34 
35 #include "configuration.h"
36 #include "debug.h"
37 #include "getpluginaddress.h"
38 
39 
40 #define OPENCBM_PLUGIN 1
41 #include "archlib.h"
42 #include "archlib-windows.h"
43 #include "libmisc.h"
44 
69 BOOL WINAPI
70 DllMain(IN HANDLE Module, IN DWORD Reason, IN LPVOID Reserved)
71 {
72  static BOOL bIsOpen = FALSE;
73 
74  FUNC_ENTER();
75 
76 #if DBG
77 
78  if (Reason == DLL_PROCESS_ATTACH)
79  {
80  // Read the debugging flags from the registry
81 
83  }
84 
85 #endif
86 
87  FUNC_LEAVE_BOOL(TRUE);
88 }
89 
90 
101 BOOL CBMAPIDECL opencbm_plugin_install_generic(const char * DefaultPluginname, unsigned int DefaultLocation)
102 {
103  BOOL error = TRUE;
104 
105  opencbm_configuration_handle configuration_handle;
106 
107  const char * configurationFilename = NULL;
108 
109  FUNC_ENTER();
110 
111  do {
112  /* create the INI file, if not present, and create the needed keys */
113 
114  configurationFilename = configuration_get_default_filename_for_install(DefaultLocation);
115  if (configurationFilename == NULL) {
116  break;
117  }
118 
119  configuration_handle = opencbm_configuration_create(configurationFilename);
120 
121  if (configuration_handle == NULL) {
122  break;
123  }
124 
125  if (DefaultPluginname != NULL) {
126  error = opencbm_configuration_set_data(configuration_handle,
127  "plugins", "default", DefaultPluginname);
128  }
129  else {
130  error = FALSE;
131  }
132 
133  error = opencbm_configuration_close(configuration_handle) || error;
134 
135  } while (0);
136 
137  cbmlibmisc_strfree(configurationFilename);
138 
139  FUNC_LEAVE_BOOL(error);
140 }
141 
142 static BOOL
143 cbm_plugin_call_self_init_plugin(const char * Pluginname, const char * Filepath, const CbmPluginInstallProcessCommandlineData_t * CommandlineData)
144 {
145  BOOL error = TRUE;
147 
148  FUNC_ENTER();
149 
150  do {
151  opencbm_plugin_self_init_plugin_t * opencbm_plugin_self_init_plugin = NULL;
152 
153  pluginHandle = plugin_load(Filepath);
154  if (pluginHandle == SHARED_OBJECT_HANDLE_INVALID) {
155  DBG_ERROR((DBG_PREFIX "Could not load plugin '%s' at '%s' for self-init!\n", Pluginname, Filepath));
156  break;
157  }
158 
159  opencbm_plugin_self_init_plugin = plugin_get_address(pluginHandle, "opencbm_plugin_self_init_plugin");
160  if ( ! opencbm_plugin_self_init_plugin ) {
161  error = FALSE;
162  break;
163  }
164 
165  error = opencbm_plugin_self_init_plugin();
166  if (error) {
167  break;
168  }
169 
170  error = plugin_set_active(Pluginname);
171 
172  } while (0);
173 
174  if (pluginHandle != SHARED_OBJECT_HANDLE_INVALID) {
175  plugin_unload(pluginHandle);
176  }
177 
178  FUNC_LEAVE_BOOL(error);
179 }
180 
191 BOOL CBMAPIDECL opencbm_plugin_install_plugin_data(const char * Pluginname, const char * Filepath, const CbmPluginInstallProcessCommandlineData_t * CommandlineData)
192 {
193  BOOL error = TRUE;
194 
195  opencbm_configuration_handle configuration_handle;
196 
197  const char * configurationFilename = NULL;
198 
199  FUNC_ENTER();
200 
201  do {
202  /* create the INI file, if not present, and create the needed keys */
203 
204  configurationFilename = configuration_get_default_filename();
205 
206  if (configurationFilename == NULL) {
207  break;
208  }
209 
210  configuration_handle = opencbm_configuration_open(configurationFilename);
211  if (configuration_handle == NULL) {
212  DBG_PRINT((DBG_PREFIX "No configuration file present. Is OpenCBM installed at all?"));
213  break;
214  }
215 
216  error = opencbm_configuration_set_data(configuration_handle,
217  Pluginname, "location", Filepath);
218 
219  if (error == 0) {
220  error = cbm_plugin_call_self_init_plugin(Pluginname, Filepath, CommandlineData->OptionMemory);
221  }
222 
223  error = opencbm_configuration_close(configuration_handle) || error;
224 
225  } while (0);
226 
227  cbmlibmisc_strfree(configurationFilename);
228 
229  FUNC_LEAVE_BOOL(error);
230 }
231 
234 
245 static int opencbm_plugin_get_all_plugin_names_callback(opencbm_configuration_handle Handle,
246  const char Section[],
247  void * Data)
248 {
249  opencbm_plugin_get_all_plugin_names_context_t * Context = Data;
250 
251  BOOL error = 0;
252 
253  FUNC_ENTER();
254 
255  do {
256  if ( ! Section || strcmp(Section, "plugins") == 0 ) {
257  break;
258  }
259 
260  /*
261  * check if the plugin is marked as active.
262  * Only active plugins are reported back,
263  */
264 
265  if ( ! plugin_is_active(Handle, Section) ) {
266  break;
267  }
268 
269  error = Context->Callback(Context->InstallParameter, Section);
270 
271  } while (0);
272 
273  FUNC_LEAVE_BOOL(error);
274 }
275 
282 EXTERN BOOL CBMAPIDECL opencbm_plugin_get_all_plugin_names(opencbm_plugin_get_all_plugin_names_context_t * Context)
283 {
284  BOOL error = TRUE;
285  opencbm_configuration_handle configuration_handle = NULL;
286  const char * configurationFilename = NULL;
287 
288  FUNC_ENTER();
289 
290  do {
291  configurationFilename = configuration_get_default_filename();
292  if (configurationFilename == NULL) {
293  break;
294  }
295 
296  configuration_handle = opencbm_configuration_open(configurationFilename);
297  if (configuration_handle == NULL) {
298  DBG_PRINT((DBG_PREFIX "No configuration file present. Is OpenCBM installed at all?"));
299  break;
300  }
301 
302  error = opencbm_configuration_enum_sections(configuration_handle,
303  opencbm_plugin_get_all_plugin_names_callback, Context);
304 
305  } while (0);
306 
307  opencbm_configuration_close(configuration_handle);
308 
309  cbmlibmisc_strfree(configurationFilename);
310 
311  FUNC_LEAVE_BOOL(error);
312 }
void cbm_get_debugging_flags(const char *ModuleName)
Set the debugging flags.
Definition: registry.c:97
void * SHARED_OBJECT_HANDLE
@@@
const char * configuration_get_default_filename_for_install(unsigned int DefaultLocation)
Get the default filename for the configuration file on installation.
Define makros for debugging purposes.
struct CbmPluginInstallProcessCommandlineData_s CbmPluginInstallProcessCommandlineData_t
Definition: instcbm.h:25
#define CBMAPIDECL
Definition: opencbm.h:85
void cbmlibmisc_strfree(const char *String)
Free a string.
Definition: libstring.c:172
#define WINAPI
Definition: opencbm.h:86
#define EXTERN
Definition: opencbm.h:84
void * plugin_get_address(SHARED_OBJECT_HANDLE handle, const char *name)
@@@
opencbm_configuration_handle opencbm_configuration_create(const char *Filename)
Creates the configuration file for reading and writing.
int opencbm_configuration_set_data(opencbm_configuration_handle Handle, const char Section[], const char Entry[], const char Value[])
Write/Change data to/in the configuration file.
opencbm_configuration_enum_sections_callback_t callback
@@@
Shared library / DLL for accessing the driver Functions for obtaining the addresses of plugin functio...
Shared library / DLL for accessing the driver Read configuration file.
#define FUNC_LEAVE_BOOL(_xxx)
Definition: debug.h:354
BOOL CBMAPIDECL opencbm_plugin_install_generic(const char *DefaultPluginname, unsigned int DefaultLocation)
@@@
#define DBG_ERROR(_xxx)
Definition: debug.h:397
EXTERN BOOL CBMAPIDECL opencbm_plugin_get_all_plugin_names(opencbm_plugin_get_all_plugin_names_context_t *Context)
@@@
void plugin_unload(SHARED_OBJECT_HANDLE handle)
@@@
#define SHARED_OBJECT_HANDLE_INVALID
@@@
SHARED_OBJECT_HANDLE plugin_load(const char *name)
@@@
#define FUNC_ENTER()
Definition: debug.h:347
BOOL WINAPI DllMain(IN HANDLE Module, IN DWORD Reason, IN LPVOID Reserved)
DLL initialization und unloading.
#define DBG_PREFIX
Definition: debug.h:320
opencbm_configuration_handle opencbm_configuration_open(const char *Filename)
Open the configuration file.
BOOL CBMAPIDECL opencbm_plugin_install_plugin_data(const char *Pluginname, const char *Filepath, const CbmPluginInstallProcessCommandlineData_t *CommandlineData)
@@@
const char * configuration_get_default_filename(void)
Get the default filename for the configuration file.
int opencbm_configuration_enum_sections_callback_t(opencbm_configuration_handle Handle, const char Section[], void *Context)
Callback type for section enumeration.
Definition: configuration.h:45
int plugin_is_active(opencbm_configuration_handle Handle, const char PluginName[])
@@@
Definition: cbm.c:55
Some functions for string handling.
#define DBG_PRINT(_xxx)
Definition: debug.h:403
int plugin_set_active(const char PluginName[])
@@@
Definition: cbm.c:93
int opencbm_configuration_enum_sections(opencbm_configuration_handle Handle, opencbm_configuration_enum_sections_callback_t Callback, void *Data)
Enumerate sections in the configuration file.
int opencbm_configuration_close(opencbm_configuration_handle Handle)
Close the configuration file.