plugin.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <dirent.h>
  6. #include <dlfcn.h>
  7. #include "list.h"
  8. #include "mapcalc.h"
  9. typedef char *(*func_t) (void);
  10. static int register_function(char *fname, void *func, char *proto);
  11. void init_plug(void);
  12. static int register_function(char *fname, void *func, char *proto)
  13. {
  14. STYP type;
  15. SYMBOL *sym;
  16. sym = getsym(fname);
  17. if (sym)
  18. symtab = (SYMBOL *) listdel((LIST *) symtab, (LIST *) sym, freesym);
  19. /*
  20. * This is quite incomplete
  21. */
  22. switch (proto[0]) {
  23. case 'd':
  24. type = st_nfunc;
  25. break;
  26. case 'm':
  27. type = st_mfunc;
  28. break;
  29. case 'p':
  30. type = st_pfunc;
  31. break;
  32. default:
  33. return 0;
  34. }
  35. sym = putsym(fname);
  36. sym->v.p = func;
  37. sym->type = sym->itype = sym->rettype = type;
  38. sym->proto = strdup(proto + 2); /* skip "m=" */
  39. return 1;
  40. }
  41. void init_plug(void)
  42. {
  43. SYMBOL *sym;
  44. char path[4096], pathname[4096], *ptr, *fname, *proto;
  45. struct dirent *ent;
  46. void *handle, *func;
  47. func_t fh, ph;
  48. DIR *dir;
  49. /*
  50. * Search algorithm:
  51. * 1. Find "pluginpath" in symbol table. If not found...
  52. * 2. Find "$GISBASE/plugins". If not defined
  53. * 3. Use "./plugins"
  54. */
  55. sym = getsym("pluginpath");
  56. if (sym && sym->v.p)
  57. strcpy(path, sym->v.p);
  58. else {
  59. ptr = getenv("GISBASE");
  60. if (ptr)
  61. strcpy(path, ptr);
  62. else
  63. strcpy(path, getcwd(path, 4096));
  64. if (path[strlen(path) - 1] != '/')
  65. strcat(path, "/");
  66. strcat(path, "plugins");
  67. }
  68. dir = opendir(path);
  69. if (!dir)
  70. return;
  71. while ((ent = readdir(dir)) != NULL) {
  72. if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
  73. continue;
  74. strcpy(pathname, path);
  75. strcat(pathname, "/");
  76. strcat(pathname, ent->d_name);
  77. if (access(pathname, R_OK | X_OK))
  78. continue;
  79. handle = dlopen(pathname, RTLD_LAZY);
  80. if (!handle)
  81. continue;
  82. fh = dlsym(handle, "fname");
  83. if (dlerror()) {
  84. dlclose(handle);
  85. continue;
  86. }
  87. fname = (*fh) ();
  88. if (!fname) {
  89. dlclose(handle);
  90. continue;
  91. }
  92. ph = dlsym(handle, "proto");
  93. if (dlerror()) {
  94. dlclose(handle);
  95. continue;
  96. }
  97. proto = (*ph) ();
  98. if (!proto) {
  99. dlclose(handle);
  100. continue;
  101. }
  102. func = dlsym(handle, fname);
  103. if (!func) {
  104. dlclose(handle);
  105. continue;
  106. }
  107. if (!register_function(fname, func, proto))
  108. dlclose(handle);
  109. }
  110. closedir(dir);
  111. }