You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.0KB

  1. /* load.c
  2. Free software by Richard W.E. Furse. Do with as you will. No
  3. warranty. */
  4. /*****************************************************************************/
  5. #include <dlfcn.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. /*****************************************************************************/
  10. #include "ladspa.h"
  11. #include "utils.h"
  12. /*****************************************************************************/
  13. /* This function provides a wrapping of dlopen(). When the filename is
  14. not an absolute path (i.e. does not begin with / character), this
  15. routine will search the LADSPA_PATH for the file. */
  16. static void *
  17. dlopenLADSPA(const char * pcFilename, int iFlag) {
  18. char * pcBuffer;
  19. const char * pcEnd;
  20. const char * pcLADSPAPath;
  21. const char * pcStart;
  22. int iEndsInSO;
  23. int iNeedSlash;
  24. size_t iFilenameLength;
  25. void * pvResult;
  26. iFilenameLength = strlen(pcFilename);
  27. pvResult = NULL;
  28. if (pcFilename[0] == '/') {
  29. /* The filename is absolute. Assume the user knows what he/she is
  30. doing and simply dlopen() it. */
  31. pvResult = dlopen(pcFilename, iFlag);
  32. if (pvResult != NULL)
  33. return pvResult;
  34. }
  35. else {
  36. /* If the filename is not absolute then we wish to check along the
  37. LADSPA_PATH path to see if we can find the file there. We do
  38. NOT call dlopen() directly as this would find plugins on the
  39. LD_LIBRARY_PATH, whereas the LADSPA_PATH is the correct place
  40. to search. */
  41. pcLADSPAPath = getenv("LADSPA_PATH");
  42. if (pcLADSPAPath) {
  43. pcStart = pcLADSPAPath;
  44. while (*pcStart != '\0') {
  45. pcEnd = pcStart;
  46. while (*pcEnd != ':' && *pcEnd != '\0')
  47. pcEnd++;
  48. pcBuffer = malloc(iFilenameLength + 2 + (pcEnd - pcStart));
  49. if (pcEnd > pcStart)
  50. strncpy(pcBuffer, pcStart, pcEnd - pcStart);
  51. iNeedSlash = 0;
  52. if (pcEnd > pcStart)
  53. if (*(pcEnd - 1) != '/') {
  54. iNeedSlash = 1;
  55. pcBuffer[pcEnd - pcStart] = '/';
  56. }
  57. strcpy(pcBuffer + iNeedSlash + (pcEnd - pcStart), pcFilename);
  58. pvResult = dlopen(pcBuffer, iFlag);
  59. free (pcBuffer);
  60. if (pvResult != NULL)
  61. return pvResult;
  62. pcStart = pcEnd;
  63. if (*pcStart == ':')
  64. pcStart++;
  65. }
  66. }
  67. }
  68. /* As a last ditch effort, check if filename does not end with
  69. ".so". In this case, add this suffix and recurse. */
  70. iEndsInSO = 0;
  71. if (iFilenameLength > 3)
  72. iEndsInSO = (strcmp(pcFilename + iFilenameLength - 3, ".so") == 0);
  73. if (!iEndsInSO) {
  74. pcBuffer = malloc(iFilenameLength + 4);
  75. strcpy(pcBuffer, pcFilename);
  76. strcat(pcBuffer, ".so");
  77. pvResult = dlopenLADSPA(pcBuffer, iFlag);
  78. free(pcBuffer);
  79. }
  80. if (pvResult != NULL)
  81. return pvResult;
  82. /* If nothing has worked, then at least we can make sure we set the
  83. correct error message - and this should correspond to a call to
  84. dlopen() with the actual filename requested. The dlopen() manual
  85. page does not specify whether the first or last error message
  86. will be kept when multiple calls are made to dlopen(). We've
  87. covered the former case - now we can handle the latter by calling
  88. dlopen() again here. */
  89. return dlopen(pcFilename, iFlag);
  90. }
  91. /*****************************************************************************/
  92. void *
  93. loadLADSPAPluginLibrary(const char * pcPluginFilename) {
  94. void * pvPluginHandle;
  95. pvPluginHandle = dlopenLADSPA(pcPluginFilename, RTLD_NOW);
  96. if (!pvPluginHandle) {
  97. fprintf(stderr,
  98. "Failed to load plugin \"%s\": %s\n",
  99. pcPluginFilename,
  100. dlerror());
  101. return NULL;
  102. }
  103. return pvPluginHandle;
  104. }
  105. /*****************************************************************************/
  106. void
  107. unloadLADSPAPluginLibrary(void * pvLADSPAPluginLibrary) {
  108. dlclose(pvLADSPAPluginLibrary);
  109. }
  110. /*****************************************************************************/
  111. const LADSPA_Descriptor *
  112. findLADSPAPluginDescriptor(void * pvLADSPAPluginLibrary,
  113. const char * pcPluginLibraryFilename,
  114. const char * pcPluginLabel) {
  115. const LADSPA_Descriptor * psDescriptor;
  116. LADSPA_Descriptor_Function pfDescriptorFunction;
  117. unsigned long lPluginIndex;
  118. dlerror();
  119. pfDescriptorFunction
  120. = (LADSPA_Descriptor_Function)dlsym(pvLADSPAPluginLibrary,
  121. "ladspa_descriptor");
  122. if (!pfDescriptorFunction) {
  123. const char * pcError = dlerror();
  124. if (pcError) {
  125. fprintf(stderr,
  126. "Unable to find ladspa_descriptor() function in plugin "
  127. "library file \"%s\": %s.\n"
  128. "Are you sure this is a LADSPA plugin file?\n",
  129. pcPluginLibraryFilename,
  130. pcError);
  131. return NULL;
  132. }
  133. }
  134. for (lPluginIndex = 0;; lPluginIndex++) {
  135. psDescriptor = pfDescriptorFunction(lPluginIndex);
  136. if (psDescriptor == NULL) {
  137. fprintf(stderr,
  138. "Unable to find label \"%s\" in plugin library file \"%s\".\n",
  139. pcPluginLabel,
  140. pcPluginLibraryFilename);
  141. return NULL;
  142. }
  143. if (strcmp(psDescriptor->Label, pcPluginLabel) == 0)
  144. return psDescriptor;
  145. }
  146. }
  147. /*****************************************************************************/
  148. /* EOF */