-
Notifications
You must be signed in to change notification settings - Fork 184
/
csmodule.c
1366 lines (1237 loc) · 45.8 KB
/
csmodule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
csmodule.c:
Copyright (C) 2005 Istvan Varga
based on dl_opcodes.c, Copyright (C) 2002 John ffitch
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
/******************************************************************************
* NEW PLUGIN INTERFACE *
* ==================== *
* *
* Plugin libraries are loaded from the directory defined by the environment *
* variable OPCODE7DIR (or the current directory if OPCODE7DIR is unset) by *
* csoundPreCompile() while initialising a Csound instance, and are unloaded *
* at the end of performance by csoundReset(). *
* A library may export any of the following five interface functions, *
* however, the presence of csoundModuleCreate() is required for identifying *
* the file as a Csound plugin module. *
* *
* int csoundModuleCreate(CSOUND *csound) (required) *
* -------------------------------------- *
* *
* Pre-initialisation function, called by csoundPreCompile(). *
* *
* int csoundModuleInit(CSOUND *csound) (optional) *
* ------------------------------------ *
* *
* Called by Csound instances before orchestra translation. One possible use *
* of csoundModuleInit() is adding new opcodes with csoundAppendOpcode(). *
* *
* int csoundModuleDestroy(CSOUND *csound) (optional) *
* --------------------------------------- *
* *
* Destructor function for Csound instance 'csound', called at the end of *
* performance, after closing audio output. *
* *
* const char *csoundModuleErrorCodeToString(int errcode) (optional) *
* ------------------------------------------------------ *
* *
* Converts error codes returned by any of the initialisation or destructor *
* functions to a string message. *
* *
* int csoundModuleInfo(void) (optional) *
* -------------------------- *
* *
* Returns information that can be used to determine if the plugin was built *
* for a compatible version of libcsound. The return value may be the sum of *
* any of the following two values: *
* *
* ((CS_APIVERSION << 16) + (CS_APISUBVER << 8)) API version *
* (int) sizeof(MYFLT) MYFLT type *
* *
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !(defined (__wasi__))
#include <setjmp.h>
#include <errno.h>
#endif
#include "csoundCore.h"
#include "csmodule.h"
#if defined(__MACH__)
#include <TargetConditionals.h>
#if (TARGET_OS_IPHONE == 0) && (TARGET_IPHONE_SIMULATOR == 0)
#if defined(MAC_OS_X_VERSION_10_6) && \
(MAC_OS_X_VERSION_MIN_REQUIRED>=MAC_OS_X_VERSION_10_6)
#define NEW_MACH_CODE
#endif
#endif
#endif
#if !(defined (__wasi__))
#if defined(LINUX) || defined(NEW_MACH_CODE) || defined(__HAIKU__)
#include <dlfcn.h>
#elif defined(WIN32)
#include <windows.h>
#endif
#endif
#if defined(HAVE_DIRENT_H)
# include <dirent.h>
# if 0 && defined(__MACH__)
typedef void* DIR;
DIR opendir(const char *);
struct dirent *readdir(DIR*);
int closedir(DIR*);
# endif
#endif
#if defined(WIN32) && !defined(__CYGWIN__)
# include <io.h>
# include <direct.h>
#endif
extern int allocgen(CSOUND *, char *, int (*)(FGDATA *, FUNC *));
/* module interface function names */
static const char *opcode_init_Name = "csound_opcode_init";
static const char *fgen_init_Name = "csound_fgen_init";
static const char *PreInitFunc_Name = "csoundModuleCreate";
static const char *InitFunc_Name = "csoundModuleInit";
static const char *DestFunc_Name = "csoundModuleDestroy";
static const char *ErrCodeToStr_Name = "csoundModuleErrorCodeToString";
static const char *InfoFunc_Name = "csoundModuleInfo";
/* environment variable storing path to plugin libraries */
static const char *plugindir_envvar = "OPCODE7DIR";
static const char *plugindir64_envvar = "OPCODE7DIR64";
/* default directory to load plugins from if environment variable is not set */
#if !(defined (__wasi__))
#ifdef __HAIKU__
# ifndef USE_DOUBLE
static char haikudirs[] = "/boot/system/lib/csound7/plugins:"
"/boot/home/config/lib/csound7/plugins:"
"/boot/system/non-packaged/lib/csound7/plugins:"
"/boot/home/config/non-packaged/lib/csound7/plugins";
# else
static char haikudirs[] = "/boot/system/lib/csound7/plugins64:"
"/boot/home/config/lib/csound7/plugins64:"
"/boot/system/non-packaged/lib/csound7/plugins64:"
"/boot/home/config/non-packaged/lib/csound7/plugins64";
# endif
# define CS_DEFAULT_PLUGINDIR haikudirs
#elif !(defined(_CSOUND_RELEASE_) && (defined(LINUX) || defined(__MACH__)))
# define ENABLE_OPCODEDIR_WARNINGS 1
# ifdef CS_DEFAULT_PLUGINDIR
# undef CS_DEFAULT_PLUGINDIR
# endif
# define CS_DEFAULT_PLUGINDIR "."
#else
# define ENABLE_OPCODEDIR_WARNINGS 0
# ifndef CS_DEFAULT_PLUGINDIR
# ifndef USE_DOUBLE
# define CS_DEFAULT_PLUGINDIR "/usr/local/lib/csound/plugins"
# else
# define CS_DEFAULT_PLUGINDIR "/usr/local/lib/csound/plugins64"
# endif
# endif
#endif
#endif
#if (TARGET_OS_IPHONE != 0) && (TARGET_IPHONE_SIMULATOR != 0)
# define ENABLE_OPCODEDIR_WARNINGS 0
#endif
typedef struct opcodeLibFunc_s {
int64_t (*opcode_init)(CSOUND *, OENTRY **); /* list of opcode entries */
NGFENS *(*fgen_init)(CSOUND *); /* list of named GEN routines */
void (*dummy)(void); /* unused */
} opcodeLibFunc_t;
typedef struct pluginLibFunc_s {
int (*InitFunc)(CSOUND *); /* initialisation routine */
int (*DestFunc)(CSOUND *); /* destructor routine */
const char *(*ErrCodeToStr)(int); /* convert error code to string */
} pluginLibFunc_t;
typedef struct csoundModule_s {
struct csoundModule_s *nxt; /* pointer to next link in chain */
void *h; /* library handle */
int (*PreInitFunc)(CSOUND *); /* pre-initialisation routine */
/* (always NULL if opcode lib) */
union {
pluginLibFunc_t p; /* generic plugin interface */
opcodeLibFunc_t o; /* opcode library interface */
} fn;
char name[1]; /* name of the module */
} csoundModule_t;
static CS_NOINLINE void print_module_error(CSOUND *csound,
const char *fmt, const char *fname,
const csoundModule_t *m, int err)
{
csoundMessageS(csound, CSOUNDMSG_ERROR, Str(fmt), fname);
if (m != NULL && m->fn.p.ErrCodeToStr != NULL)
csoundMessageS(csound, CSOUNDMSG_ERROR,
": %s\n", Str(m->fn.p.ErrCodeToStr(err)));
else
csoundMessageS(csound, CSOUNDMSG_ERROR, "\n");
}
static int check_plugin_compatibility(CSOUND *csound, const char *fname, int n)
{
int myfltSize, minorVersion, majorVersion;
myfltSize = n & 0xFF;
if (UNLIKELY(myfltSize != 0 && myfltSize != (int) sizeof(MYFLT))) {
csoundWarning(csound, Str("not loading '%s' (uses incompatible "
"floating point type)"), fname);
return -1;
}
if (UNLIKELY(n & (~0xFF))) {
minorVersion = (n & 0xFF00) >> 8;
majorVersion = (n & (~0xFFFF)) >> 16;
if (majorVersion != (int) CS_APIVERSION ||
(minorVersion > (int) CS_APISUBVER)) { /* NOTE **** REFACTOR *** */
csoundWarning(csound, Str("not loading '%s' (incompatible "
"with this version of Csound (%d.%d/%d.%d)"),
fname, majorVersion,minorVersion,
CS_APIVERSION,CS_APISUBVER);
return -1;
}
}
return 0;
}
/**
* Initialise a single module.
* Return value is CSOUND_SUCCESS if there was no error.
*/
static CS_NOINLINE int csoundInitModule(CSOUND *csound, csoundModule_t *m)
{
int i;
if (m->PreInitFunc != NULL) {
if (m->fn.p.InitFunc != NULL) {
i = m->fn.p.InitFunc(csound);
if (UNLIKELY(i != 0)) {
print_module_error(csound, Str("Error starting module '%s'"),
&(m->name[0]), m, i);
return CSOUND_ERROR;
}
}
}
else {
/* deal with fgens if there are any */
if (m->fn.o.fgen_init != NULL) {
NGFENS *names = m->fn.o.fgen_init(csound);
for (i = 0; names[i].name != NULL; i++)
allocgen(csound, names[i].name, names[i].fn);
}
if (m->fn.o.opcode_init != NULL) {
OENTRY *opcodlst_n;
int64_t length;
/* load opcodes */
if (UNLIKELY((length = m->fn.o.opcode_init(csound, &opcodlst_n)) < 0L))
return CSOUND_ERROR;
else {
length /= (long) sizeof(OENTRY);
if (length) {
if (UNLIKELY(csoundAppendOpcodes(csound, opcodlst_n,
(int) length) != 0))
return CSOUND_ERROR;
}
}
}
}
return CSOUND_SUCCESS;
}
#ifdef __wasi__
__attribute__((used))
void csoundWasiLoadPlugin(CSOUND *csound, void *preInitFunc, void *initFunc, void *destFunc, void *errCodeToStr) {
csoundModule_t *module = csound->Malloc(csound, sizeof(csoundModule_t) + 1);
module->h = (void*) NULL;
// The javascript host must assert that this is provided
module->PreInitFunc = (int (*)(CSOUND *)) preInitFunc;
if (initFunc) {
module->fn.p.InitFunc = (int (*)(CSOUND *)) initFunc;
}
if (destFunc) {
module->fn.p.DestFunc = (int (*)(CSOUND *)) destFunc;
}
if (errCodeToStr) {
module->fn.p.ErrCodeToStr = (const char *(*)(int)) errCodeToStr;
}
module->nxt = (csoundModule_t*) csound->csmodule_db;
csound->csmodule_db = module;
module->PreInitFunc(csound);
}
__attribute__((used))
void csoundWasiLoadOpcodeLibrary(CSOUND *csound, void *fgenInitFunc, void *opcodeInitFunc) {
csoundModule_t *module = csound->Malloc(csound, sizeof(csoundModule_t) + 1);
module->h = (void*) NULL;
if (fgenInitFunc) {
module->fn.o.fgen_init = (NGFENS *(*)(CSOUND *)) fgenInitFunc;
}
if (opcodeInitFunc) {
module->fn.o.opcode_init = (int64_t (*)(CSOUND *, OENTRY **)) opcodeInitFunc;
}
module->nxt = (csoundModule_t*) csound->csmodule_db;
csound->csmodule_db = module;
}
__attribute__((used))
int csoundDestroyModules(CSOUND *csound) {
csoundModule_t *m;
int i;
int retval = CSOUND_SUCCESS;
while (csound->csmodule_db != NULL) {
m = (csoundModule_t*) csound->csmodule_db;
/* call destructor functions */
if (m->PreInitFunc != NULL && m->fn.p.DestFunc != NULL) {
i = m->fn.p.DestFunc(csound);
if (UNLIKELY(i != 0)) {
print_module_error(csound, Str("Error de-initialising module '%s'"),
&(m->name[0]), m, i);
retval = CSOUND_ERROR;
}
}
csound->csmodule_db = (void*) m->nxt;
/* free memory used by database */
csound->Free(csound, (void*) m);
}
/* return with error code */
return retval;
}
__attribute__((used))
int csoundInitModules(CSOUND *csound) {
csoundModule_t *m;
int i, retval = CSOUND_SUCCESS;
for (m = (csoundModule_t*) csound->csmodule_db; m != NULL; m = m->nxt) {
i = csoundInitModule(csound, m);
if (UNLIKELY(i != CSOUND_SUCCESS && i < retval))
retval = i;
}
/* return with error code */
return retval;
}
// In browser-wasi, this function is replaced
// by the js-host.
__attribute__((used))
extern int csoundLoadModules(CSOUND *csound);
int csoundLoadAndInitModules(CSOUND *csound, const char *opdir) {
return 0;
}
#else /* __wasi__ */
/* load a single plugin library, and run csoundModuleCreate() if present */
/* returns zero on success */
static CS_NOINLINE int csoundLoadExternal(CSOUND *csound,
const char *libraryPath)
{
csoundModule_t m;
volatile jmp_buf tmpExitJmp;
csoundModule_t *mp;
char *fname;
void *h, *p;
int (*infoFunc)(void);
int err;
/* check for a valid name */
if (UNLIKELY(libraryPath == NULL || libraryPath[0] == '\0'))
return CSOUND_ERROR;
/* remove leading directory components from name */
fname = (char*) libraryPath + (int) strlen(libraryPath);
for ( ; fname[0] != DIRSEP && fname != (char*) libraryPath; fname--)
;
if (fname[0] == DIRSEP)
fname++;
if (UNLIKELY(fname[0] == '\0'))
return CSOUND_ERROR;
/* load library */
/* #if defined(LINUX) */
//printf("About to open library '%s'\n", libraryPath);
/* #endif */
err = csoundOpenLibrary(&h, libraryPath);
if (UNLIKELY(err)) {
char ERRSTR[256];
#if (defined(LINUX) || defined(__HAIKU__))
snprintf(ERRSTR, 256, Str("could not open library '%s' (%s)"),
libraryPath, dlerror());
#else
snprintf(ERRSTR, 256, Str("could not open library '%s' (%d)"),
libraryPath, err);
#endif
if (csound->delayederrormessages == NULL) {
csound->delayederrormessages = csound->Malloc(csound, strlen(ERRSTR)+1);
strcpy(csound->delayederrormessages, ERRSTR);
}
else {
char *new =
csound->ReAlloc(csound, csound->delayederrormessages,
strlen(csound->delayederrormessages)+strlen(ERRSTR)+11);
if (UNLIKELY(new==NULL)) {
csound->Free(csound, csound->delayederrormessages);
return CSOUND_ERROR;
}
csound->delayederrormessages = new;
strcat(csound->delayederrormessages, "\nWARNING: ");
strcat(csound->delayederrormessages, ERRSTR);
}
return CSOUND_ERROR;
}
/* check if the library is compatible with this version of Csound */
infoFunc = (int (*)(void)) csoundGetLibrarySymbol(h, InfoFunc_Name);
if (infoFunc != NULL) {
if (UNLIKELY(check_plugin_compatibility(csound, fname, infoFunc()) != 0)) {
csoundCloseLibrary(h);
return CSOUND_ERROR;
}
}
/* was this plugin already loaded ? */
for (mp = (csoundModule_t*) csound->csmodule_db; mp != NULL; mp = mp->nxt) {
if (UNLIKELY(mp->h == h)) {
csoundCloseLibrary(h);
return CSOUND_SUCCESS;
}
}
/* find out if it is a Csound plugin */
memset(&m, 0, sizeof(csoundModule_t));
m.h = h;
m.PreInitFunc =
(int (*)(CSOUND *)) csoundGetLibrarySymbol(h, PreInitFunc_Name);
if (m.PreInitFunc != NULL) {
/* generic plugin library */
m.fn.p.InitFunc =
(int (*)(CSOUND *)) csoundGetLibrarySymbol(h, InitFunc_Name);
m.fn.p.DestFunc =
(int (*)(CSOUND *)) csoundGetLibrarySymbol(h, DestFunc_Name);
m.fn.p.ErrCodeToStr =
(const char *(*)(int)) csoundGetLibrarySymbol(h, ErrCodeToStr_Name);
}
else {
/* opcode library */
m.fn.o.opcode_init =
(int64_t (*)(CSOUND *, OENTRY **))
csoundGetLibrarySymbol(h, opcode_init_Name);
m.fn.o.fgen_init =
(NGFENS *(*)(CSOUND *)) csoundGetLibrarySymbol(h, fgen_init_Name);
if (UNLIKELY(m.fn.o.opcode_init == NULL && m.fn.o.fgen_init == NULL)) {
/* must have csound_opcode_init() or csound_fgen_init() */
csoundCloseLibrary(h);
if (UNLIKELY(csound->oparms->msglevel & 0x400))
csound->Warning(csound, Str("'%s' is not a Csound plugin library"),
libraryPath);
return CSOUND_ERROR;
}
}
/* set up module info structure */
/* (note: space for NUL character is already included in size of struct) */
p = (void*) csound->Malloc(csound,
sizeof(csoundModule_t) + (size_t) strlen(fname));
if (UNLIKELY(p == NULL)) {
csoundCloseLibrary(h);
csound->ErrorMsg(csound,
"%s", Str("csoundLoadExternal(): memory allocation failure"));
return CSOUND_MEMORY;
}
mp = (csoundModule_t*) p;
memcpy(mp, &m, sizeof(csoundModule_t));
strcpy(&(mp->name[0]), fname);
/* link into database */
mp->nxt = (csoundModule_t*) csound->csmodule_db;
csound->csmodule_db = (void*) mp;
/* call csoundModuleCreate() if available */
if (m.PreInitFunc != NULL) {
memcpy((void*) &tmpExitJmp, (void*) &csound->exitjmp, sizeof(jmp_buf));
if ((err = setjmp(csound->exitjmp)) != 0) {
memcpy((void*) &csound->exitjmp, (void*) &tmpExitJmp, sizeof(jmp_buf));
print_module_error(csound, Str("Error in pre-initialisation function "
"of module '%s'"), fname, NULL, 0);
return (err == (CSOUND_EXITJMP_SUCCESS + CSOUND_MEMORY) ?
CSOUND_MEMORY : CSOUND_INITIALIZATION);
}
err = m.PreInitFunc(csound);
memcpy((void*) &csound->exitjmp, (void*) &tmpExitJmp, sizeof(jmp_buf));
if (UNLIKELY(err != 0)) {
print_module_error(csound, Str("Error in pre-initialisation function "
"of module '%s'"), fname, &m, err);
return CSOUND_INITIALIZATION;
}
}
/* plugin was loaded successfully */
return CSOUND_SUCCESS;
}
static int csoundCheckOpcodeDeny(CSOUND * csound, const char *fname)
{
/* Check to see if the fname is on the do-not-load list */
char buff[256];
char *th;
char *p, *deny;
char *list = getenv("CS_OMIT_LIBS");
/* printf("DEBUG %s(%d): check fname=%s\n", __FILE__, __LINE__, fname); */
/* printf("DEBUG %s(%d): list %s\n", __FILE__, __LINE__, list); */
if (list==NULL) return 0;
strNcpy(buff, fname, 255); //buff[255]='\0';
strrchr(buff, '.')[0] = '\0'; /* Remove .so etc */
p = cs_strdup(csound, list);
deny = cs_strtok_r(p, ",", &th);
/* printf("DEBUG %s(%d): check buff=%s\n", __FILE__, __LINE__, deny); */
while (deny) {
/* printf("DEBUG %s(%d): deny=%s\n", __FILE__, __LINE__, deny); */
if (strcmp(deny, buff)==0) {
csound->Free(csound, p);
/* printf("DEBUG %s(%d): found\n", __FILE__, __LINE__); */
return 1;
}
deny = cs_strtok_r(NULL, ",", &th);
}
csound->Free(csound, p);
/* printf("DEBUG %s(%d): not found\n", __FILE__, __LINE__); */
return 0;
}
static int _dir_exists(char *path) {
// returns 1 if path is a directory and it exists
struct stat s;
int err = stat(path, &s);
return (err == 0 && S_ISDIR(s.st_mode)) ? 1 : 0;
}
/**
* Load plugin libraries for Csound instance 'csound', and call
* pre-initialisation functions.
* Return value is CSOUND_SUCCESS if there was no error, CSOUND_ERROR if
* some modules could not be loaded or initialised, and CSOUND_MEMORY
* if a memory allocation failure has occured.
*/
int csoundLoadModules(CSOUND *csound)
{
#if (defined(HAVE_DIRENT_H) && (TARGET_OS_IPHONE == 0))
DIR *dir;
struct dirent *f;
const char *dname, *fname;
enum { searchpath_buflen = 2048, buflen = 1024 };
char buf[buflen];
int i, n, len, err = CSOUND_SUCCESS;
char *dname1, *end;
int read_directory = 1;
char searchpath_buf[searchpath_buflen];
char sep =
#ifdef WIN32
';';
#else
':';
#endif
#ifdef __HAIKU__
int dfltdir = 0;
#endif
if (UNLIKELY(csound->csmodule_db != NULL))
return CSOUND_ERROR;
/* open plugin directory */
dname = csoundGetEnv(csound, (sizeof(MYFLT) == sizeof(float) ?
plugindir_envvar : plugindir64_envvar));
if (dname == NULL) {
#if ENABLE_OPCODEDIR_WARNINGS
csound->opcodedirWasOK = 0;
# ifdef USE_DOUBLE
dname = csoundGetEnv(csound, plugindir_envvar);
if (dname == NULL)
# endif
#endif
#ifdef CS_DEFAULT_PLUGINDIR
dname = CS_DEFAULT_PLUGINDIR;
#ifdef __HAIKU__
dfltdir = 1;
#endif
#else
dname = "";
#endif
}
/* opcodedir GLOBAL override **experimental** */
if (csound->opcodedir != NULL) {
dname = csound->opcodedir;
csound->Message(csound, "OPCODEDIR overridden to %s \n", dname);
}
size_t pos = strlen(dname);
char *userplugindir = getenv("CS_USER_PLUGINDIR");
// The user set a search path for plugins via an env variable. Paths here
// should be absolute and should not need variable expansion
if(userplugindir != NULL) {
snprintf(searchpath_buf, searchpath_buflen, "%s%c%s", dname, sep, userplugindir);
dname = searchpath_buf;
} else {
#ifdef CS_DEFAULT_USER_PLUGINDIR
// Use default user path
// In this case, userplugindir is a relative path to a prefix wich needs
// to be expanded
userplugindir = CS_DEFAULT_USER_PLUGINDIR;
#if defined(LINUX) || defined(__MACH__)
char *prefix = getenv("HOME");
#elif defined(WIN32)
char *prefix = getenv("LOCALAPPDATA");
#endif
// VL: need to check so we don't get a segfault with NULL strings
size_t prefixlen = prefix ? strlen(prefix) : 0;
size_t userplugindirlen = userplugindir ? strlen(userplugindir) : 0;
if(pos + prefixlen + 2 > searchpath_buflen - 1) {
csound->ErrorMsg(csound, "%s", Str("Plugins search path too long\n"));
} else if(userplugindirlen + prefixlen + 1 >= buflen) {
csound->ErrorMsg(csound, "%s", Str("User plugin dir too long\n"));
} else {
snprintf(buf, buflen, "%s/%s", prefix, userplugindir);
if(_dir_exists(buf)) {
snprintf(searchpath_buf, searchpath_buflen, "%s%c%s", dname, sep, buf);
dname = searchpath_buf;
}
}
#endif
}
if(UNLIKELY(csound->oparms->odebug))
csound->Message(csound, Str("Plugins search path: %s\n"), dname);
/* We now loop through the directory list */
while(read_directory) {
/* find separator */
if((end = strchr(dname, sep)) != NULL) {
*end = '\0';
/* copy directory name */
dname1 = cs_strdup(csound, (char *) dname);
*end = sep; /* restore for re-execution */
/* move to next directory name */
dname = end + 1;
} else {
/* copy last directory name) */
dname1 = cs_strdup(csound, (char *) dname);
read_directory = 0;
}
/* protect for the case where there is an
extra separator at the end */
if(*dname1 == '\0') {
csound->Free(csound, dname1);
break;
}
dir = opendir(dname1);
if (UNLIKELY(dir == (DIR*) NULL)) {
#if defined(__HAIKU__)
if(!dfltdir)
#endif
csound->Warning(csound, Str("Error opening plugin directory '%s': %s"),
dname1, strerror(errno));
csound->Free(csound, dname1);
continue;
}
if(UNLIKELY(csound->oparms->odebug))
csound->Message(csound, "Opening plugin directory: %s\n", dname1);
/* load database for deferred plugin loading */
/* n = csoundLoadOpcodeDB(csound, dname); */
/* if (n != 0) */
/* return n; */
/* scan all files in directory */
while ((f = readdir(dir)) != NULL) {
fname = &(f->d_name[0]);
if (UNLIKELY(fname[0]=='_')) continue;
n = len = (int) strlen(fname);
if (UNLIKELY(fname[0]=='_')) continue;
#if defined(WIN32)
strcpy(buf, "dll");
n -= 4;
#elif defined(__MACH__)
strcpy(buf, "dylib");
n -= 6;
#else
strcpy(buf, "so");
n -= 3;
#endif
if (n <= 0 || fname[n] != '.')
continue;
i = 0;
do {
if (UNLIKELY((fname[++n] | (char) 0x20) != buf[i]))
break;
} while (buf[++i] != '\0');
if (buf[i] != '\0')
continue;
/* found a dynamic library, attempt to open it */
if (UNLIKELY(((int) strlen(dname) + len + 2) > 1024)) {
csound->Warning(csound, Str("path name too long, skipping '%s'"),
fname);
continue;
}
/* printf("DEBUG %s(%d): possibly deny %s\n", __FILE__, __LINE__,fname); */
if (UNLIKELY(csoundCheckOpcodeDeny(csound, fname))) {
csoundWarning(csound, Str("Library %s omitted\n"), fname);
continue;
}
snprintf(buf, 1024, "%s%c%s", dname1, DIRSEP, fname);
if (UNLIKELY(csound->oparms->odebug)) {
csoundMessage(csound, Str("Loading '%s'\n"), buf);
}
n = csoundLoadExternal(csound, buf);
if (UNLIKELY(UNLIKELY(n == CSOUND_ERROR)))
continue; /* ignore non-plugin files */
if (UNLIKELY(n < err))
err = n; /* record serious errors */
}
closedir(dir);
csound->Free(csound, dname1);
}
return (err == CSOUND_INITIALIZATION ? CSOUND_ERROR : err);
#else
return CSOUND_SUCCESS;
#endif /* HAVE_DIRENT_H */
}
static int cmp_func(const void *p1, const void *p2)
{
return (strcmp(*((const char**) p1), *((const char**) p2)));
}
int csoundLoadExternals(CSOUND *csound)
{
char *s, **lst;
int i, cnt, err;
s = csound->dl_opcodes_oplibs;
if (UNLIKELY(s == NULL || s[0] == '\0'))
return 0;
/* IV - Feb 19 2005 */
csound->dl_opcodes_oplibs = NULL;
csoundMessage(csound, "%s", Str("Loading command-line libraries:\n"));
cnt = 1;
i = 0;
do {
if (s[i] == ',')
cnt++;
} while (s[++i] != '\0');
lst = (char**) csound->Malloc(csound, sizeof(char*) * cnt);
i = cnt = 0;
lst[cnt++] = s;
do {
if (s[i] == ',') {
lst[cnt++] = &(s[i + 1]);
s[i] = '\0';
}
} while (s[++i] != '\0');
qsort((void*) lst, (size_t) cnt, sizeof(char*), cmp_func);
i = 0;
do {
char *fname = lst[i];
if (fname[0] != '\0' && !(i && strcmp(fname, lst[i - 1]) == 0)) {
err = csoundLoadExternal(csound, fname);
if (UNLIKELY(err == CSOUND_INITIALIZATION || err == CSOUND_MEMORY))
csoundDie(csound, Str(" *** error loading '%s'"), fname);
else if (!err)
csoundMessage(csound, " %s\n", fname);
}
} while (++i < cnt);
/* file list is no longer needed */
csound->Free(csound, lst);
csound->Free(csound, s);
return 0;
}
/**
* Call initialisation functions of all loaded modules that have a
* csoundModuleInit symbol, for Csound instance 'csound'.
* Return value is CSOUND_SUCCESS if there was no error, and CSOUND_ERROR if
* some modules could not be initialised.
*/
int csoundInitModules(CSOUND *csound)
{
csoundModule_t *m;
int i, retval = CSOUND_SUCCESS;
/* call init functions */
for (m = (csoundModule_t*) csound->csmodule_db; m != NULL; m = m->nxt) {
i = csoundInitModule(csound, m);
if (UNLIKELY(i != CSOUND_SUCCESS && i < retval))
retval = i;
}
/* return with error code */
return retval;
}
/* load a plugin library and also initialise it */
/* called on deferred loading of opcode plugins */
int csoundLoadAndInitModule(CSOUND *csound, const char *fname)
{
volatile jmp_buf tmpExitJmp;
volatile int err;
err = csoundLoadExternal(csound, fname);
if (UNLIKELY(err != 0))
return err;
memcpy((void*) &tmpExitJmp, (void*) &csound->exitjmp, sizeof(jmp_buf));
if (UNLIKELY((err = setjmp(csound->exitjmp)) != 0)) {
memcpy((void*) &csound->exitjmp, (void*) &tmpExitJmp, sizeof(jmp_buf));
return (err == (CSOUND_EXITJMP_SUCCESS + CSOUND_MEMORY) ?
CSOUND_MEMORY : CSOUND_INITIALIZATION);
}
/* NOTE: this depends on csound->csmodule_db being the most recently */
/* loaded plugin library */
err = csoundInitModule(csound, (csoundModule_t*) csound->csmodule_db);
memcpy((void*) &csound->exitjmp, (void*) &tmpExitJmp, sizeof(jmp_buf));
return err;
}
int csoundLoadAndInitModules(CSOUND *csound, const char *opdir)
{
#if (defined(HAVE_DIRENT_H) && (TARGET_OS_IPHONE == 0))
DIR *dir;
struct dirent *f;
const char *dname, *fname;
char buf[1024];
int i, n, len, err = CSOUND_SUCCESS;
char *dname1, *end;
int read_directory = 1;
char sep =
#ifdef WIN32
';';
#else
':';
#endif
#ifdef __HAIKU__
int dfltdir = 0;
#endif
// VL: check is not wanted
//if (UNLIKELY(csound->csmodule_db != NULL))
///return CSOUND_ERROR;
/* open plugin directory */
// EM'2021: This seems to be dead code since opdir will never be NULL and
// the value of dname will be discarded, see "dname = opdir" later
dname = csoundGetEnv(csound, (sizeof(MYFLT) == sizeof(float) ?
plugindir_envvar : plugindir64_envvar));
if (dname == NULL) {
#if ENABLE_OPCODEDIR_WARNINGS
csound->opcodedirWasOK = 0;
# ifdef USE_DOUBLE
dname = csoundGetEnv(csound, plugindir_envvar);
if (dname == NULL)
# endif
#endif
#ifdef CS_DEFAULT_PLUGINDIR
dname = CS_DEFAULT_PLUGINDIR;
#ifdef __HAIKU__
dfltdir = 1;
#endif
#else
dname = "";
#endif
}
if (opdir != NULL) {
dname = opdir;
}
/* We now loop through the directory list */
while(read_directory) {
/* find separator */
if((end = strchr(dname, sep)) != NULL) {
*end = '\0';
/* copy directory name */
dname1 = cs_strdup(csound, (char *) dname);
*end = sep; /* restore for re-execution */
/* move to next directory name */
dname = end + 1;
} else {
/* copy last directory name) */
dname1 = cs_strdup(csound, (char *) dname);
read_directory = 0;
}
/* protect for the case where there is an
extra separator at the end */
if(*dname1 == '\0') {
csound->Free(csound, dname1);
break;
}
dir = opendir(dname1);
if (UNLIKELY(dir == (DIR*) NULL)) {
#if defined(__HAIKU__)
if(!dfltdir)
#endif
csound->Warning(csound, Str("Error opening plugin directory '%s': %s"),
dname1, strerror(errno));
csound->Free(csound, dname1);
continue;
}
if(UNLIKELY(csound->oparms->odebug))
csound->Message(csound, "Opening plugin directory: %s\n", dname1);
/* load database for deferred plugin loading */
/* n = csoundLoadOpcodeDB(csound, dname); */
/* if (n != 0) */
/* return n; */
/* scan all files in directory */
while ((f = readdir(dir)) != NULL) {
fname = &(f->d_name[0]);
if (UNLIKELY(fname[0]=='_')) continue;
n = len = (int) strlen(fname);
if (UNLIKELY(fname[0]=='_')) continue;
#if defined(WIN32)
strcpy(buf, "dll");
n -= 4;
#elif defined(__MACH__)
strcpy(buf, "dylib");
n -= 6;
#else
strcpy(buf, "so");
n -= 3;
#endif
if (n <= 0 || fname[n] != '.')
continue;
i = 0;
do {
if (UNLIKELY((fname[++n] | (char) 0x20) != buf[i]))
break;
} while (buf[++i] != '\0');
if (buf[i] != '\0')
continue;
/* found a dynamic library, attempt to open it */
if (UNLIKELY(((int) strlen(dname) + len + 2) > 1024)) {
csound->Warning(csound, Str("path name too long, skipping '%s'"),
fname);
continue;
}
/* printf("DEBUG %s(%d): possibly deny %s\n", __FILE__, __LINE__,fname); */
if (UNLIKELY(csoundCheckOpcodeDeny(csound, fname))) {
csoundWarning(csound, Str("Library %s omitted\n"), fname);
continue;
}
snprintf(buf, 1024, "%s%c%s", dname1, DIRSEP, fname);
if (UNLIKELY(csound->oparms->odebug)) {
csoundMessage(csound, Str("Loading '%s'\n"), buf);
}
n = csoundLoadAndInitModule(csound, buf);
if (UNLIKELY(UNLIKELY(n == CSOUND_ERROR)))
continue; /* ignore non-plugin files */
if (UNLIKELY(n < err))
err = n; /* record serious errors */
}
closedir(dir);
csound->Free(csound, dname1);
}
return (err == CSOUND_INITIALIZATION ? CSOUND_ERROR : err);
#else
return CSOUND_SUCCESS;
#endif /* HAVE_DIRENT_H */
}
/**
* Call destructor functions of all loaded modules that have a
* csoundModuleDestroy symbol, for Csound instance 'csound'.
* Return value is CSOUND_SUCCESS if there was no error, and
* CSOUND_ERROR if some modules could not be de-initialised.
*/
extern int sfont_ModuleDestroy(CSOUND *csound);
int csoundDestroyModules(CSOUND *csound)
{
csoundModule_t *m;
int i, retval;
retval = CSOUND_SUCCESS;