/
ff_modules.c
1231 lines (1010 loc) · 29.3 KB
/
ff_modules.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
/*
** Formatting: tabwidth=4 shiftwidth=4
**
** File: ff_modules.c - tightly dependent on narray.c::Narray
**
** Contains code for loading vanilla modules.
**
*/
//parser actually pulls in narray which pulls in cvector meh
#include "cvector.h"
#include "narray.h"
#include "parser.h"
#include <errno.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef __MINGW32__
#include <dirent.h>
#endif
#ifdef _WIN32
#include <io.h>
#define PATH_SEPARATOR ";"
#else
#define PATH_SEPARATOR ":"
#include <dirent.h>
#include <libgen.h>
#endif /* _WIN32 */
#include "module_io.h"
/*
** Value of debug must be greater than DEBUG_THRESHOLD to produce
** debug info.
*/
#define DEBUG_THRESHOLD 2
/*
** A global list of modules currently loaded and available.
*/
Narray* loaded_modules = NULL;
static const char ver_any_str[] = "(any)";
#define printable_ver_str(ver) ((ver) ? (ver) : ver_any_str)
/* davinci module file extension */
static const char DVM_EXT[] = "la";
/* Environment variable for module lookup */
static const char DV_MOD_PATH_ENV[] = "DV_MOD_PATH";
/* Default module lookup path if the environment is not set */
static const char DV_MOD_PATH_DEF[] = DV_MOD_PATH;
static char DV_MODULE_INIT_FUNC_NAME[] = "dv_module_init";
static char DV_MODULE_FINI_FUNC_NAME[] = "dv_module_fini";
typedef int (*dvModuleInitFunc)(const char* called_as, dvModuleInitStuff* init_stuff);
typedef int (*dvModuleFiniFunc)(const char* called_as);
static int load_dv_module(char* mod_fname, char* ver, int offset, dvModule* mod);
static int init_dv_module(dvModule* m);
Var* new_module(char* module_name);
static int load_dv_module_functions(dvModule* m);
static void free_module_function(vfuncptr fptr);
static int autoload_dep(dvDepAttr* dep, int ndep);
void del_module(Var* mv);
static int uninit_dv_module(dvModule* m);
int unload_dv_module(char* module_name);
/*******************************************************************/
/** davinci load_module(mod="module_name") function. **/
/*******************************************************************/
Var* ff_load_dv_module(vfuncptr func, Var* args)
{
int ac;
Var** av;
Var* v_return = NULL;
char* module_name = NULL;
Alist alist[2]; /* arguments list */
alist[0] = make_alist("mod", ID_STRING, NULL, &module_name);
alist[1].name = NULL;
if (parse_args(func, args, alist) == 0) {
parse_error("%s(): argument parsing failed.", func->name);
return NULL;
}
if (module_name == NULL) {
parse_error("%s(): \"%s\" must be specified.", func->name, alist[0].name);
return NULL;
}
if (get_global_sym(module_name)) {
parse_error(
"%s(): Variable %s already exists in global space. "
"Module load aborted.",
func->name, module_name);
return NULL;
}
v_return = new_module(module_name);
if (v_return) {
/*
** Actually load the module into the davinci variable
** just created.
*/
if (!load_dv_module(module_name, NULL, 1, &V_MODULE(v_return))) {
free_var(v_return);
return NULL;
}
/* stick the symbol into the symbol table */
/* sym_put(global_scope(), v_return); */
put_global_sym(v_return);
V_MODULE(v_return).stage = MOD_VAR_ADDED;
}
return v_return;
/* return NULL; */
}
/*******************************************************************/
/** davinci unload_module(mod="module_name") function. **/
/*******************************************************************/
Var* ff_unload_dv_module(vfuncptr func, Var* args)
{
int ac;
Var** av;
char* module_name = NULL;
Alist alist[2]; /* arguments list */
alist[0] = make_alist("mod", ID_STRING, NULL, &module_name);
alist[1].name = NULL;
if (parse_args(func, args, alist) == 0) {
parse_error("%s(): argument parsing failed.", func->name);
return NULL;
}
if (module_name == NULL) {
parse_error("%s(): \"%s\" must be specified.", func->name, alist[0].name);
return NULL;
}
unload_dv_module(module_name);
return NULL;
}
/*******************************************************************/
/** davinci list_modules() function. **/
/*******************************************************************/
Var* ff_list_dv_modules(vfuncptr func, Var* args)
{
char* mod_name;
Var* mod_var;
Var* mlst;
int i, n;
char** mnlst;
int len;
dvModule* m;
Var* v = NULL;
char* module_name = NULL;
Alist alist[2]; /* arguments list */
alist[0] = make_alist("module", ID_STRING, NULL, &module_name);
alist[1].name = NULL;
if (parse_args(func, args, alist) == 0) {
parse_error("%s(): argument parsing failed.", func->name);
return NULL;
}
if (module_name != NULL) {
// Find all the stuff in the specified module.
// Locate the module
if (Narray_find(loaded_modules, module_name, (void*)&v) != -1) {
char* func_name;
m = &(V_MODULE(v));
n = Narray_count(m->functions);
mnlst = (char**)calloc(n, sizeof(char*));
for (i = 0; i < n; i++) {
Narray_get(m->functions, i, &func_name, NULL);
mnlst[i] = strdup(func_name);
}
return (newText(n, mnlst));
} else {
parse_error("Unable to find module %s\n", module_name);
return (NULL);
}
} else {
n = Narray_count(loaded_modules);
mnlst = (char**)calloc(n, sizeof(char*));
if (mnlst == NULL) {
return NULL;
}
for (i = 0; i < n; i++) {
Narray_get(loaded_modules, i, &mod_name, (void**)&mod_var);
m = &V_MODULE(mod_var);
len = strlen(mod_name) + 1;
if (m->ver) {
len += strlen(m->ver) + 1;
}
mnlst[i] = (char*)calloc(len, sizeof(char));
if (mnlst[i] == NULL) { /* memory failure */
parse_error("Mem allocation error.\n");
while (--i >= 0) {
free(mnlst[i]);
}
free(mnlst);
return NULL;
}
if (m->ver && strcmp(m->ver, "") == 0) {
/* include version */
sprintf(mnlst[i], "%s.%s", mod_name, m->ver);
} else {
/* no version associated with the module */
sprintf(mnlst[i], "%s", mod_name);
}
}
mlst = newText(n, mnlst);
return mlst;
}
}
/***************************************************************/
/** Interface Functions **/
/***************************************************************/
Var* pp_call_dv_module_func(vfuncptr f, Var* args)
{
/*
** Call function with the specified args-list and return
** whatever the function returns.
*/
return (f->fptr)(f, args);
}
void print_module(dvModule* m)
{
int i, n;
char* func_name;
parse_error("Module %s:", m->name);
if (m->ver) {
parse_error("\tversion: %s", m->ver);
}
parse_error("\tfull path: %s", m->path);
n = m->init_stuff.ndep;
parse_error("\t%d dependencies:", n);
for (i = 0; i < n; i++) {
parse_error("\t\t%s ver:%s", m->init_stuff.dep[i].name,
printable_ver_str(m->init_stuff.dep[i].ver));
}
n = Narray_count(m->functions);
parse_error("\t%d functions:", n);
for (i = 0; i < n; i++) {
Narray_get(m->functions, i, &func_name, NULL);
parse_error("\t\t%s", func_name);
}
}
void pp_print_module_var(Var* mv)
{
print_module(&V_MODULE(mv));
}
/*
** This function keeps track of the module dependencies
** according to modules that have been loaded thus far.
*/
int add_to_list_of_loaded_modules(Var* v)
{
if (loaded_modules == NULL) {
loaded_modules = Narray_create(10);
}
if (Narray_add(loaded_modules, v->name, v) == -1) {
/* this element may already exist */
if (Narray_find(loaded_modules, v->name, NULL) != -1) {
parse_error("Module %s is already loaded. Unload+Reload to refresh.", v->name);
return 0;
} else {
parse_error("Probably a mem allocation error while loading module %s.", v->name);
return 0;
}
}
if (debug > DEBUG_THRESHOLD) {
parse_error("Added module %s to the list of loaded modules.", v->name);
}
return 1; /* success */
}
Var* search_in_list_of_loaded_modules(char* name)
{
Var* v;
if (loaded_modules == NULL || name == NULL) return NULL;
if (Narray_find(loaded_modules, name, (void**)&v) != -1) {
return v;
}
return NULL;
}
/*
** Complimentry function of add_to_list_of_loaded_modules()
*/
void remove_from_list_of_loaded_modules(Var* v)
{
int i;
if (loaded_modules == NULL) {
if (debug > DEBUG_THRESHOLD) {
parse_error(
"Cannot remove %s from the list of loaded modules. "
"Reason: The list is empty.",
v->name);
}
return;
}
if (v->name == NULL) {
if (debug > DEBUG_THRESHOLD) {
parse_error(
"Cannot remove a module with (null) name "
"from the list of active modules.");
}
return;
}
if ((i = Narray_find(loaded_modules, v->name, NULL)) != -1) {
/*
** Specified module exists in the list of loaded modules.
** Remove it!
*/
Narray_delete(loaded_modules, v->name);
} else {
parse_error(
"Cannot remove %s from the list of loaded modules. "
"Reason: Module is not loaded.",
v->name);
}
if (debug > DEBUG_THRESHOLD) {
parse_error("Removing %s from the list of loaded modules.", v->name);
}
}
/*
** Create a davinci module variable with the specified name.
*/
Var* new_module(char* module_name)
{
Var* v;
if (module_name == NULL) {
if (debug > DEBUG_THRESHOLD) {
parse_error("Cannot create a module with (null) name.");
}
return NULL;
}
if (search_in_list_of_loaded_modules(module_name) != NULL) {
/* If module already loaded, don't load it again */
parse_error("Module %s already loaded.", module_name);
return NULL;
}
v = newVar();
memset(&V_MODULE(v), 0, sizeof(dvModule));
/* make scope not clean this variable */
mem_claim(v);
V_TYPE(v) = ID_MODULE;
V_NAME(v) = strdup(module_name);
if (V_NAME(v) == NULL) {
parse_error("Mem allocation error in new_module().");
free_var(v);
return NULL;
}
V_MODULE(v).functions = Narray_create(10);
V_MODULE(v).name = V_NAME(v);
/* keep track of available modules */
if (!add_to_list_of_loaded_modules(v)) {
/* Addition into the list of loaded modules failed. Abort. */
free_var(v);
return NULL;
}
V_MODULE(v).stage = MOD_LST_ADDED;
return v;
}
/*
** CAUTION!
** Verify module dependencies before calling this function.
**
** Delete the "dvModule" portion of a Var*
** It gets called from free_var() -- which is called at various
** points in this file.
*/
void del_module(Var* v)
{
dvModule* m;
if (debug > DEBUG_THRESHOLD) {
parse_error("Deleting variable %s.", v->name);
}
m = &V_MODULE(v);
if (m->stage >= MOD_INIT) {
/* if module's init function was called, call the uninit function */
uninit_dv_module(m);
}
if (m->stage >= MOD_LST_ADDED) {
/* remove the module from the list of loaded functions */
if (v->name) {
remove_from_list_of_loaded_modules(v);
}
}
if (m->stage >= MOD_VAR_ADDED) {
/* remove module variable from the global symbol table */
rm_symtab(v);
}
if (m->functions) {
Narray_free(m->functions, (Narray_FuncPtr)free_module_function);
}
if (m->path) {
free(m->path);
}
if (m->handle) {
close_dv_module_file(m->handle);
}
}
int print_dependents(char* module_name)
{
int i, n;
int j, m;
int ct;
Var* v;
char* dep_name;
ct = 0; /* keep a count of dependents */
if (VERBOSE) {
parse_error("Dependents of %s:", module_name);
}
n = Narray_count(loaded_modules);
for (i = 0; i < n; i++) {
Narray_get(loaded_modules, i, &dep_name, (void**)&v);
m = V_MODULE(v).init_stuff.ndep;
for (j = 0; j < m; j++) {
if (strcmp(V_MODULE(v).init_stuff.dep[j].name, module_name) == 0) {
if (VERBOSE) {
parse_error("%s%s", ((ct == 0) ? "" : ", "), dep_name);
}
ct++;
}
}
}
if (VERBOSE) {
parse_error("Total %d dependents.", ct);
}
return ct;
}
/*
** Implementation of ff_unload_dv_module() above.
*/
int unload_dv_module(char* module_name)
{
Var* v;
/* v = get_global_sym(module_name); */
v = search_in_list_of_loaded_modules(module_name);
if (v) {
if (VERBOSE) {
parse_error("Unloading module %s.", module_name);
}
/*
** Print module dependencies. So that the user knows
** that unloding this module can cause problems.
*/
print_dependents(module_name);
/*
** free_var() bounces back to ff_modules.c::del_module() above
** where uninit_dv_module() is called.
*/
free_var(v);
} else {
parse_error("Module %s is not loaded. So, it cannot be unloaded.", module_name);
}
return 1;
}
/*
** Search for a function within a module.
*/
vfuncptr find_module_func(dvModule* m, char* name)
{
int idx;
vfuncptr fptr;
if (m == NULL || name == NULL) return NULL;
if ((idx = Narray_find(m->functions, name, (void**)&fptr)) == -1) {
return NULL; /* not found! */
}
return fptr;
}
/***************************************************************/
/** implementation functions **/
/***************************************************************/
static int uninit_dv_module(dvModule* m /* module to be uninitialized */
)
{
dvModuleFiniFunc fini_func;
fini_func = (dvModuleFiniFunc)locate_dv_module_func_in_slib(m->handle, DV_MODULE_FINI_FUNC_NAME);
if (fini_func == NULL) {
if (debug > DEBUG_THRESHOLD) {
parse_error("Cannot find module uninit function %s for %s. Reason: %s.",
DV_MODULE_FINI_FUNC_NAME, m->name, strerror(errno));
}
} else {
(fini_func)(m->name);
}
return 1; /* success */
}
static void free_module_function(vfuncptr fptr)
{
/* free(fptr->name); */
free(fptr);
}
/*
** Add the specified function into the name addressable
** array.
*/
static vfuncptr add_func_to_module(dvModule* m, vfuncptr func)
{
vfuncptr old;
int i;
if (Narray_add(m->functions, func->name, func) == -1) {
/* this element may already exist */
if ((i = Narray_find(m->functions, func->name, NULL)) != -1) {
Narray_replace(m->functions, i, func, (void**)&old);
if (old) {
return old;
}
} else {
return NULL;
}
}
return func;
}
/*
** Extract module's version from its name file name.
*/
static char* extract_dv_mod_ver_from_fname(const char* mod_name, char* mod_file_name)
{
char* p;
if (mod_file_name) {
/* skip the name and ".dvm" extension */
p = &mod_file_name[strlen(mod_name) + 1 + strlen(DVM_EXT)];
/* skip the first dot -- if there is one */
if (*p) p++;
return p;
}
return NULL;
}
static struct dirent* find_next_file_with_prefix(DIR* d, const char* pfx)
{
struct dirent* de;
int pfx_len = strlen(pfx);
errno = 0; /* reset error number */
while ((de = readdir(d))) {
if (strncmp(de->d_name, pfx, pfx_len) == 0) {
return de;
}
}
return NULL;
}
static int cmp_mod_ver(const char* v1, const char* v2)
{
if (v1 == NULL && v2 == NULL) return 0;
if (v1 == NULL && v2 != NULL) return -1;
if (v1 != NULL && v2 == NULL) return 1;
return strcmp(v1, v2);
}
/*
** Returns the fully qualified path name of the latest module file
** if one exists. The name returned is allocated using malloc(). It
** is the caller's responsibility to free() it.
**
** Returns NULL if the name is not found or an error occurrs.
*/
// path: A ":" separated list of dirs to find the name in */
static int get_module_versions(const char* mod_name, char* path, cvector_str* modules)
{
#define MAX_STR_LEN 1024
char* q;
DIR* d;
struct dirent* de;
char* dir_name;
int strsz;
char name[MAX_STR_LEN];
char fullpath[MAX_STR_LEN];
char* fname;
// Process each of the directories in the search path
// to find the module with the latest version.
if ((strsz = snprintf(name, 1024, "%s.%s", mod_name, DVM_EXT)) >= MAX_STR_LEN) {
parse_error("Module name is too long (limit 1023 chars):\n%s.%s\n", dir_name, fname);
return 0;
}
for (q = path; (dir_name = strtok(q, PATH_SEPARATOR)); q = NULL) {
d = opendir(dir_name);
if (d == NULL) {
// Directory does not exist: warn and continue.
if (debug > DEBUG_THRESHOLD) {
parse_error("Unusable dir %s in path. Reason: %s.", dir_name, strerror(errno));
}
continue;
}
errno = 0; // clear error indicator
while ((de = find_next_file_with_prefix(d, name))) {
fname = de->d_name;
if ((strsz = snprintf(fullpath, 1024, "%s/%s", dir_name, fname)) >= MAX_STR_LEN) {
parse_error("Module path is too long (limit 1023 chars):\n%s/%s\n", dir_name, fname);
break;
}
cvec_push_str(modules, fullpath);
}
}
closedir(d);
return modules->size;
}
static char* locate_latest_dv_module_in_path(const char* mod_name, char* path)
{
char* latest = NULL;
char *vcurr = NULL, *vlatest = NULL;
char name[1024];
char* fname;
cvector_str modules;
cvec_str(&modules, 0, 16);
int nmodules = get_module_versions(mod_name, path, &modules);
for (int i = 0; i < nmodules; i++) {
fname = modules.a[i];
vcurr = extract_dv_mod_ver_from_fname(mod_name, fname);
if (cmp_mod_ver(vcurr, vlatest) > 0) {
if (latest) free(latest);
latest = strdup(fname);
vlatest = vcurr;
}
}
cvec_free_str(&modules);
return latest;
}
/*
** Returns the fully qualified path name of the latest module file
** if one exists. The name returned is allocated using malloc(). It
** is the caller's responsibility to free() it.
**
** Returns NULL if the name is not found or an error occurrs.
*/
// offset meaning:
// == 0 match version exactly
// > 0 find latest version >= ver
// < 0 find latest version <= ver
static char* locate_versioned_dv_module_in_path(const char* mod_name, const char* ver, int offset, char* path)
{
char* q;
DIR* d;
struct dirent* de;
char* latest = NULL;
int latest_i = -1;
char *vcurr = NULL, *vlatest = NULL;
char* dir_name;
int strsz;
int replace_reqd;
char name[1024];
char* fname;
int i;
cvector_str modules;
cvec_str(&modules, 0, 16);
int nmodules = get_module_versions(mod_name, path, &modules);
for (i = 0; i < nmodules; i++) {
fname = modules.a[i];
vcurr = extract_dv_mod_ver_from_fname(mod_name, fname);
replace_reqd = 0;
if ((offset == 0 && cmp_mod_ver(vcurr, ver) == 0) ||
(offset > 0 && cmp_mod_ver(vcurr, ver) >= 0 && cmp_mod_ver(vcurr, vlatest) > 0) ||
(offset < 0 && cmp_mod_ver(vcurr, ver) <= 0 && cmp_mod_ver(vcurr, vlatest) > 0)) {
latest_i = i;
vlatest = vcurr;
}
}
if (latest_i >= 0) latest = strdup(modules.a[i]);
cvec_free_str(&modules);
return latest;
}
/*
** Locate the specified module using the name and the
** version information provided. Use the shell variable
** DV_MOD_PATH for a list of directories to look at, if
** defined. If the variable is not set, use a default
** path.
**
** On success a path to the specified file is returned. It
** is allocated using malloc(). It is the caller's responsibility
** to free it.
**
** On error NULL is returned.
*/
// ver == NULL get latest
// offset same as above function if ver != NULL
static char* locate_dv_module(const char* name, const char* ver, int offset)
{
char* dv_mod_path = NULL;
const char* dv_mod_path_save = NULL;
char* loc = NULL;
dv_mod_path_save = dv_mod_path = getenv(DV_MOD_PATH_ENV);
if (dv_mod_path == NULL) {
if (debug > DEBUG_THRESHOLD) {
parse_error("%s env is not set using default path %s.", DV_MOD_PATH_ENV, DV_MOD_PATH_DEF);
}
/* If DV_MOD_PATH environment is not set use the default path */
dv_mod_path_save = DV_MOD_PATH_DEF;
dv_mod_path = strdup(DV_MOD_PATH_DEF);
} else {
if (debug > DEBUG_THRESHOLD) {
parse_error("%s env found as being set to %s.", DV_MOD_PATH_ENV, dv_mod_path);
}
dv_mod_path = strdup(dv_mod_path);
}
if (dv_mod_path == NULL) {
parse_error("Mem allocation error.");
return NULL;
}
if (ver == NULL) {
loc = locate_latest_dv_module_in_path(name, dv_mod_path);
} else {
loc = locate_versioned_dv_module_in_path(name, ver, offset, dv_mod_path);
}
if (loc) {
parse_error("Loading module %s from %s.", name, loc);
} else {
parse_error("Module %s not found in path %s.", name, dv_mod_path_save);
}
free(dv_mod_path);
return loc;
}
/*
** load_dv_module() calls open_dv_module() and init_dv_module()
** to complete the loading of a module.
*/
/* name of the module being loaded < mod_fname */
/* module version = NULL for "unspecified/any" */
// offset see above
static int load_dv_module(char* mod_name, char* mod_ver, int offset, dvModule* mod)
{
dvModuleHandle* mh;
// Look thru DV_MOD_PATH and locate the first occurrance
// of the specified module.
mod->path = locate_dv_module(mod_name, mod_ver, offset);
if (mod->path == NULL) {
parse_error("Unable to locate module %s.", mod_name);
return 0;
}
mod->stage = MOD_LOCATED;
if (mod->path == NULL) {
parse_error("Mem allocation error, while loading %s.", mod_name);
return 0;
}
mod->handle = open_dv_module_file(mod->path);
if (mod->handle == NULL) {
parse_error("Unable to open module file %s. See previous messages.", mod->path);
return 0;
}
mod->stage = MOD_OPENED;
mod->name = strdup(mod_name);
if (mod->name == NULL) {
parse_error("Mem allocation error.");
// next two lines are taken care of in del_module() as well
// close_dv_module_file(mod->handle); mod->handle = NULL;
// free(mod_path); mod_path = NULL;
return 0;
}
// Extract module version from module's name
// mymodule.dvm.1.0 -> 1.0
// hismodule.dvm -> (null)
//
// We could make the following line conditional on
// mod_ver != NULL.
mod->ver = extract_dv_mod_ver_from_fname(mod_name, basename(mod->path));
if (!mod->ver) {
parse_error("Unable to extract module version for %s. See previous messages.", mod->path);
/* next three lines are taken care of in del_module() as well --
close_dv_module_file(mod->handle); mod->handle = NULL;
free(mod->name); mod->name = NULL;
free(mod->path); mod->path = NULL;
*/
return 0;
}
// Initialize the module by calling its initialization function.
// and loading the functions exported by the module. At the
// end of init_dv_module(), the "mod" variable will have the
// "init_stuff" filled in and the functions exported by the
// module loaded in "mod.functions".
if (!init_dv_module(mod)) {
parse_error("Module initialization failed for %s. See previous messages. ", mod->name);
// next three lines are taken care of in del_module() as well
// close_dv_module_file(mod->handle); mod->handle = NULL;
// free(mod->name); mod->name = NULL;
// free(mod->path); mod->path = NULL;
return 0;
}
mod->stage = MOD_INIT;
/* load module functions into a name addressable array */
if (!load_dv_module_functions(mod)) {
parse_error("Loading of module functions failed for %s. ", mod->name);
parse_error("See previous messages.");
/* del_module() takes care of the functions loaded thus far */
return 0;
}
mod->stage = MOD_FUNC_LOADED;
/* ????????????????????????????????????????????????????????
** MAY BE: autoload_dep() should be moved to init_dv_module
** or the function loading part moved out of init_dv_module
*/
if (!autoload_dep(mod->init_stuff.dep, mod->init_stuff.ndep)) {
parse_error(
"Atleast one of the module dependencies "
"for %s v:%s failed loading. See previous messages.",
mod->name, printable_ver_str(mod->ver));
/* next three lines are taken care of in del_module() as well --
close_dv_module_file(mod->handle); mod->handle = NULL;
free(mod->name); mod->name = NULL;
free(mod->path); mod->path = NULL;
*/
/*
** Return whatever we have till now. Let the user take
** care of the freeing the memory by deleting the variable.
*/
return 0;
}
mod->stage = MOD_DEP_LOADED;
return 1;
}
static int autoload_dv_module(char* name, const char* ver)
{
Var* v_return;
if (VERBOSE) {
parse_error("autoloading %s ...", name);
}
/*
** TODO:
**
** Create a davinci module variable. Pass that variable
** into load_module() to actually load the module. Check
** the error returned by load_module() if any. On error
** collapse the module we were trying to load.
*/
if (get_global_sym(name)) {
parse_error(
"Variable %s already exists in global space. "
"Module load aborted.",
name);
return 0;
}
v_return = new_module(name);
if (v_return) {
/*
** Actually load the module into the davinci variable
** just created.
*/
if (!load_dv_module(V_NAME(v_return), NULL, 1, &V_MODULE(v_return))) {