gwenhywfar  4.3.3
plugin.c
Go to the documentation of this file.
1 /***************************************************************************
2  $RCSfile$
3  -------------------
4  cvs : $Id$
5  begin : Thu Apr 03 2003
6  copyright : (C) 2003 by Martin Preuss
7  email : martin@libchipcard.de
8 
9  ***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2.1 of the License, or (at your option) any later version. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19  * Lesser General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU Lesser General Public *
22  * License along with this library; if not, write to the Free Software *
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
24  * MA 02111-1307 USA *
25  * *
26  ***************************************************************************/
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 #include "plugin_p.h"
33 #include <gwenhywfar/buffer.h>
34 #include <gwenhywfar/debug.h>
35 #include <gwenhywfar/directory.h>
36 #include <gwenhywfar/pathmanager.h>
37 #include <gwenhywfar/gwenhywfar.h>
38 
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44 #include <errno.h>
45 #include <string.h>
46 #ifdef HAVE_STRINGS_H
47 # include <strings.h>
48 #endif
49 #include <ctype.h>
50 
51 #ifdef OS_WIN32
52 # include <windows.h>
53 #endif
54 
55 static GWEN_PLUGIN_MANAGER_LIST *gwen_plugin_manager__list=0;
56 
57 
59 GWEN_LIST_FUNCTIONS(GWEN_PLUGIN, GWEN_Plugin)
61 GWEN_LIST_FUNCTIONS(GWEN_PLUGIN_MANAGER, GWEN_PluginManager)
62 
63 
64 
66  gwen_plugin_manager__list=GWEN_PluginManager_List_new();
67  return 0;
68 }
69 
70 
71 
73  GWEN_PluginManager_List_free(gwen_plugin_manager__list);
74  return 0;
75 }
76 
77 
78 
80  const char *name,
81  const char *fileName){
82  GWEN_PLUGIN *p;
83 
84  assert(pm);
85  assert(name);
87  DBG_MEM_INC("GWEN_PLUGIN", 0);
88  p->refCount=1;
91  p->manager=pm;
92  p->name=strdup(name);
93  if (fileName)
94  p->fileName=strdup(fileName);
95 
96  return p;
97 }
98 
99 
100 
102  if (p) {
103  DBG_MEM_DEC("GWEN_PLUGIN");
104  assert(p->refCount);
105  if (--(p->refCount)==0) {
107  free(p->name);
108  free(p->fileName);
109  if (p->libLoader) {
110  GWEN_LibLoader_CloseLibrary(p->libLoader);
111  GWEN_LibLoader_free(p->libLoader);
112  }
114  GWEN_FREE_OBJECT(p);
115  } /* if refCount reaches zero */
116  } /* if p */
117 }
118 
119 
120 
122  assert(p);
123  assert(p->refCount);
124  DBG_MEM_INC("GWEN_PLUGIN", 1);
125  p->refCount++;
126 }
127 
128 
129 
131  assert(p);
132  return p->manager;
133 }
134 
135 
136 
137 const char *GWEN_Plugin_GetName(const GWEN_PLUGIN *p){
138  assert(p);
139  return p->name;
140 }
141 
142 
143 
144 const char *GWEN_Plugin_GetFileName(const GWEN_PLUGIN *p){
145  assert(p);
146  return p->fileName;
147 }
148 
149 
150 
152  assert(p);
153  return p->libLoader;
154 }
155 
156 
157 
159  assert(p);
160  p->libLoader=ll;
161 }
162 
163 
164 
165 
166 
167 
168 
169 
171  const char *destLib){
173 
174  assert(name);
175  assert(destLib);
177  DBG_MEM_INC("GWEN_PLUGIN_MANAGER", 0);
180  pm->name=strdup(name);
181  pm->destLib=strdup(destLib);
182  pm->plugins=GWEN_Plugin_List_new();
183 
184  return pm;
185 }
186 
187 
188 
190  if (pm) {
191  DBG_MEM_DEC("GWEN_PLUGIN_MANAGER");
192  GWEN_Plugin_List_free(pm->plugins);
194  free(pm->destLib);
195  free(pm->name);
197  GWEN_FREE_OBJECT(pm);
198  }
199 }
200 
201 
202 
204  assert(pm);
205  return pm->name;
206 }
207 
208 
209 
211  const char *callingLib,
212  const char *s){
213  assert(pm);
214  return GWEN_PathManager_AddPath(callingLib,
215  pm->destLib,
216  pm->name,
217  s);
218 }
219 
220 
221 
223  const char *callingLib,
224  const char *s,
226  assert(pm);
227  return GWEN_PathManager_AddRelPath(callingLib,
228  pm->destLib,
229  pm->name,
230  s,
231  rm);
232 }
233 
234 
235 
237  const char *callingLib,
238  const char *s) {
239  assert(pm);
240  return GWEN_PathManager_InsertPath(callingLib,
241  pm->destLib,
242  pm->name,
243  s);
244 }
245 
246 
247 
249  const char *callingLib,
250  const char *s) {
251  assert(pm);
252  return GWEN_PathManager_RemovePath(callingLib,
253  pm->destLib,
254  pm->name,
255  s);
256 }
257 
258 
259 
260 #ifdef OS_WIN32
262  const char *callingLib,
263  const char *keypath,
264  const char *varname){
265  HKEY hkey;
266  TCHAR nbuffer[MAX_PATH];
267  BYTE vbuffer[MAX_PATH];
268  DWORD nsize;
269  DWORD vsize;
270  DWORD typ;
271  int i;
272 
273  assert(pm);
274 
275  snprintf(nbuffer, sizeof(nbuffer), keypath);
276 
277  /* open the key */
278  if (RegOpenKey(HKEY_LOCAL_MACHINE, nbuffer, &hkey)){
279  DBG_INFO(GWEN_LOGDOMAIN, "RegOpenKey %s failed.", keypath);
280  return 1;
281  }
282 
283  /* find the variablename */
284  for (i=0;; i++) {
285  nsize=sizeof(nbuffer);
286  vsize=sizeof(vbuffer);
287  if (ERROR_SUCCESS!=RegEnumValue(hkey,
288  i, /* index */
289  nbuffer,
290  &nsize,
291  0, /* reserved */
292  &typ,
293  vbuffer,
294  &vsize))
295  break;
296  if (strcasecmp(nbuffer, varname)==0 && typ==REG_SZ) {
297  /* variable found */
298  RegCloseKey(hkey);
299  return GWEN_PathManager_AddPath(callingLib,
300  pm->destLib,
301  pm->name,
302  (char*)vbuffer);
303  }
304  } /* for */
305 
306  RegCloseKey(hkey);
308  "In RegKey \"%s\" the variable \"%s\" does not exist",
309  keypath, varname);
310  return 1;
311 
312 }
313 
314 #else /* OS_WIN32 */
315 
317  GWEN_UNUSED const char *callingLib,
318  GWEN_UNUSED const char *keypath,
319  GWEN_UNUSED const char *varname){
320  return 0;
321 }
322 #endif /* OS_WIN32 */
323 
324 
325 
327  const char *modname){
328  GWEN_LIBLOADER *ll;
329  GWEN_PLUGIN *plugin;
331  void *p;
332  GWEN_BUFFER *nbuf;
333  const char *s;
334  const char *fname;
335  int err;
336  GWEN_STRINGLIST *sl;
338 
339  assert(pm);
340  ll=GWEN_LibLoader_new();
341  sl=GWEN_PathManager_GetPaths(pm->destLib, pm->name);
342  if (sl==NULL) {
343  DBG_ERROR(GWEN_LOGDOMAIN, "No paths for plugins (%s)", pm->name);
345  return NULL;
346  }
347  nbuf=GWEN_Buffer_new(0, 128, 0, 1);
348  s=modname;
349  while(*s) GWEN_Buffer_AppendByte(nbuf, tolower(*(s++)));
351  fname=0;
352  while(se) {
353  fname=GWEN_StringListEntry_Data(se);
354  assert(fname);
356  GWEN_Buffer_GetStart(nbuf))==0)
357  break;
358  else {
360  "Could not load plugin \"%s\" from \"%s\"", modname, fname);
361  }
363  }
364  if (!se) {
365  DBG_ERROR(GWEN_LOGDOMAIN, "Plugin \"%s\" not found.", modname);
366  GWEN_Buffer_free(nbuf);
369  return NULL;
370  }
371  GWEN_Buffer_free(nbuf);
372 
373  /* create name of init function */
374  nbuf=GWEN_Buffer_new(0, 128, 0, 1);
375  s=pm->name;
376  while(*s) GWEN_Buffer_AppendByte(nbuf, tolower(*(s++)));
377  GWEN_Buffer_AppendByte(nbuf, '_');
378  s=modname;
379  while(*s) GWEN_Buffer_AppendByte(nbuf, tolower(*(s++)));
380  GWEN_Buffer_AppendString(nbuf, "_factory");
381 
382  /* resolve name of factory function */
383  err=GWEN_LibLoader_Resolve(ll, GWEN_Buffer_GetStart(nbuf), &p);
384  if (err) {
386  GWEN_Buffer_free(nbuf);
390  return 0;
391  }
392  GWEN_Buffer_free(nbuf);
393 
394  fn=(GWEN_PLUGIN_FACTORYFN)p;
395  assert(fn);
396  plugin=fn(pm, modname, fname);
397  if (!plugin) {
398  DBG_ERROR(GWEN_LOGDOMAIN, "Error in plugin: No plugin created");
402  return 0;
403  }
404 
405  /* store libloader */
407  GWEN_Plugin_SetLibLoader(plugin, ll);
408  return plugin;
409 }
410 
411 
412 
414  const char *modname,
415  const char *fname){
416  GWEN_LIBLOADER *ll;
417  GWEN_PLUGIN *plugin;
419  void *p;
420  GWEN_BUFFER *nbuf;
421  const char *s;
422  int err;
423 
424  ll=GWEN_LibLoader_new();
425  if (GWEN_LibLoader_OpenLibrary(ll, fname)) {
427  "Could not load plugin \"%s\" (%s)", modname, fname);
429  return 0;
430  }
431 
432  /* create name of init function */
433  nbuf=GWEN_Buffer_new(0, 128, 0, 1);
434  s=pm->name;
435  while(*s) GWEN_Buffer_AppendByte(nbuf, tolower(*(s++)));
436  GWEN_Buffer_AppendByte(nbuf, '_');
437  s=modname;
438  while(*s) GWEN_Buffer_AppendByte(nbuf, tolower(*(s++)));
439  GWEN_Buffer_AppendString(nbuf, "_factory");
440 
441  /* resolve name of factory function */
442  err=GWEN_LibLoader_Resolve(ll, GWEN_Buffer_GetStart(nbuf), &p);
443  if (err) {
445  GWEN_Buffer_free(nbuf);
448  return 0;
449  }
450  GWEN_Buffer_free(nbuf);
451 
452  fn=(GWEN_PLUGIN_FACTORYFN)p;
453  assert(fn);
454  plugin=fn(pm, modname, fname);
455  if (!plugin) {
456  DBG_INFO(GWEN_LOGDOMAIN, "Error in plugin: No plugin created");
459  return 0;
460  }
461 
462  /* store libloader */
463  GWEN_Plugin_SetLibLoader(plugin, ll);
464 
465  return plugin;
466 }
467 
468 
469 
471  const char *s){
472  GWEN_PLUGIN *p;
473 
474  assert(pm);
475  p=GWEN_Plugin_List_First(pm->plugins);
476  while(p) {
477  if (strcasecmp(p->name, s)==0)
478  break;
479  p=GWEN_Plugin_List_Next(p);
480  }
481 
482  return p;
483 }
484 
485 
486 
488  const char *s){
489  GWEN_PLUGIN *p;
490 
492  if (p)
493  return p;
495  if (p) {
496  GWEN_Plugin_List_Add(p, pm->plugins);
497  return p;
498  }
499  DBG_INFO(GWEN_LOGDOMAIN, "Plugin \"%s\" not found", s);
500  return 0;
501 }
502 
503 
504 
507 
508  pm=GWEN_PluginManager_List_First(gwen_plugin_manager__list);
509  while(pm) {
510  if (strcasecmp(pm->name, s)==0)
511  break;
512  pm=GWEN_PluginManager_List_Next(pm);
513  }
514 
515  return pm;
516 }
517 
518 
519 
521  GWEN_PLUGIN_MANAGER *tpm;
522  int rv;
523 
525  assert(pm);
527  if (tpm) {
529  "Plugin type \"%s\" already registered",
530  pm->name);
531  return -1;
532  }
533 
534  rv=GWEN_PathManager_DefinePath(pm->destLib, pm->name);
535  if (rv) {
536  DBG_INFO(GWEN_LOGDOMAIN, "Could not define path for plugin [%s:%s]",
537  pm->destLib, pm->name);
538  return rv;
539  }
540 
541  GWEN_PluginManager_List_Add(pm, gwen_plugin_manager__list);
543  "Plugin type \"%s\" registered",
544  pm->name);
545  return 0;
546 }
547 
548 
549 
551  GWEN_PLUGIN_MANAGER *tpm;
552  int rv;
553 
555  assert(pm);
557  if (!tpm) {
559  "Plugin type \"%s\" not registered",
560  pm->name);
561  return -1;
562  }
563 
564  rv=GWEN_PathManager_UndefinePath(pm->destLib, pm->name);
565  if (rv) {
566  DBG_INFO(GWEN_LOGDOMAIN, "Could not undefine path for plugin [%s:%s]",
567  pm->destLib, pm->name);
568  return rv;
569  }
570 
571  GWEN_PluginManager_List_Del(pm);
573  "Plugin type \"%s\" unregistered",
574  pm->name);
575  return 0;
576 }
577 
578 
579 
580 GWEN_PLUGIN_DESCRIPTION_LIST2*
582  GWEN_PLUGIN_DESCRIPTION_LIST2 *pl;
583  GWEN_STRINGLIST *sl;
585 
586  sl=GWEN_PathManager_GetPaths(pm->destLib, pm->name);
587  if (sl==NULL) {
588  DBG_ERROR(GWEN_LOGDOMAIN, "No paths for plugins (%s)", pm->name);
589  return NULL;
590  }
592  if (!se) {
593  DBG_ERROR(GWEN_LOGDOMAIN, "No paths given");
595  return 0;
596  }
597 
598  pl=GWEN_PluginDescription_List2_new();
599  while(se) {
600  int rv;
601  const char *path;
602 
603  path=GWEN_StringListEntry_Data(se);
604  assert(path);
605  rv=GWEN_LoadPluginDescrsByType(path, pm->name, pl);
606  if (rv) {
608  "Error loading plugin description in \"%s\"", path);
609  }
611  } /* while */
612 
613  if (GWEN_PluginDescription_List2_GetSize(pl)==0) {
614  GWEN_PluginDescription_List2_free(pl);
616  return 0;
617  }
618 
620  return pl;
621 }
622 
623 
625  assert(pm);
626  return GWEN_PathManager_GetPaths(pm->destLib, pm->name);
627 }
628 
629 
630 
633  const char *modName) {
634  GWEN_PLUGIN_DESCRIPTION_LIST2 *dl;
635 
637  if (dl==0)
638  return 0;
639  else {
640  GWEN_PLUGIN_DESCRIPTION_LIST2_ITERATOR *dit;
641 
642  dit=GWEN_PluginDescription_List2_First(dl);
643  if (dit) {
645 
646  d=GWEN_PluginDescription_List2Iterator_Data(dit);
647  while(d) {
648  if (strcasecmp(GWEN_PluginDescription_GetName(d), modName)==0)
649  break;
650  d=GWEN_PluginDescription_List2Iterator_Next(dit);
651  }
652  GWEN_PluginDescription_List2Iterator_free(dit);
653 
654  if (d) {
657  return d;
658  }
659  }
661  }
662 
663  return 0;
664 }
665 
666 
667 
669 #if 0
670  DBG_ERROR(0, "Adding plugin [%s] of type [%s]",
671  p->name, pm->name);
672 #endif
673  GWEN_Plugin_List_Add(p, pm->plugins);
674 }
675 
676 
677 
678 
679 
680