Added patch by Siddharth Heroor
[Klink.git] / dynload.c
blob0fa633d021952fe38f6128e6b5bba5d18577451e
1 /*_. Kernel 0.0: dynload.c */
2 /*_ , Header */
3 /*_ . Purpose */
4 /* Dynamic Loader for Kernel */
6 /*_ . Credits */
7 /* See HISTORY.org */
8 #define _KLINK_SOURCE
10 /*_ . Includes */
11 #include "dynload.h"
12 #include <string.h>
13 #include <stdio.h>
14 #include <stdlib.h>
16 #ifndef MAXPATHLEN
17 # define MAXPATHLEN 1024
18 #endif /* */
19 static void make_filename (const char *name, char *filename);
20 static void make_init_fn (const char *name, char *init_fn);
22 /*_ . System-specific includes */
23 /*_ , WIN32 */
24 #ifdef _WIN32
25 # include <windows.h>
27 /*_ , Otherwise assume SUN */
28 #else
29 typedef void *HMODULE;
30 typedef void (*FARPROC) ();
32 #define SUN_DL
33 #include <dlfcn.h>
34 #endif
36 /*_ , Body */
37 /*_ . For Win32 */
38 #ifdef _WIN32
40 #define PREFIX ""
41 #define SUFFIX ".dll"
42 static void
43 display_w32_error_msg (const char *additional_message)
45 LPVOID msg_buf;
46 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM |
47 FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, GetLastError (), 0,
48 (LPTSTR) & msg_buf, 0, NULL);
49 fprintf (stderr, "klink load-extension: %s: %s", additional_message,
50 msg_buf);
51 LocalFree (msg_buf);
52 } static HMODULE
54 dl_attach (const char *module)
56 HMODULE dll = LoadLibrary (module);
57 if (!dll)
58 display_w32_error_msg (module);
59 return dll;
62 static FARPROC
63 dl_proc (HMODULE mo, const char *proc)
65 FARPROC procedure = GetProcAddress (mo, proc);
66 if (!procedure)
67 display_w32_error_msg (proc);
68 return procedure;
71 static void
72 dl_detach (HMODULE mo)
74 (void) FreeLibrary (mo);
77 /*_ . For SUN */
78 #elif defined(SUN_DL)
80 #include <dlfcn.h>
82 #define PREFIX "lib"
83 #define SUFFIX ".so"
84 static HMODULE
85 dl_attach (const char *module)
87 HMODULE so = dlopen (module, RTLD_LAZY);
88 if (!so)
90 fprintf (stderr, "Error loading klink extension \"%s\": %s\n", module,
91 dlerror ());
93 return so;
96 static FARPROC
97 dl_proc (HMODULE mo, const char *proc)
99 const char *errmsg;
100 FARPROC fp = (FARPROC) dlsym (mo, proc);
101 if ((errmsg = dlerror ()) == 0)
103 return fp;
105 fprintf (stderr, "Error initializing kernel module \"%s\": %s\n", proc,
106 errmsg);
107 return 0;
110 static void
111 dl_detach (HMODULE mo)
113 (void) dlclose (mo);
115 #endif /* */
117 klink_load_ext (klink * sc, pko args)
119 pko first_arg;
120 pko retval;
121 char filename[MAXPATHLEN], init_fn[MAXPATHLEN + 6];
122 char *name;
123 HMODULE dll_handle;
124 void (*module_init) (klink * sc);
125 if ((args != K_NIL) && is_string ((first_arg = pair_car (sc,args))))
127 name = string_value (first_arg);
128 make_filename (name, filename);
129 make_init_fn (name, init_fn);
130 dll_handle = dl_attach (filename);
131 if (dll_handle == 0)
133 retval = K_F;
136 else
138 module_init = (void (*)(klink *)) dl_proc (dll_handle, init_fn);
139 if (module_init != 0)
141 (*module_init) (sc);
142 retval = K_T;
145 else
147 retval = K_F;
152 else
154 retval = K_F;
156 return (retval);
159 static void
160 make_filename (const char *name, char *filename)
162 strcpy (filename, name);
163 strcat (filename, SUFFIX);
164 } static void
166 make_init_fn (const char *name, char *init_fn)
168 const char *p = strrchr (name, '/');
169 if (p == 0)
171 p = name;
173 else
175 p++;
177 strcpy (init_fn, "init_");
178 strcat (init_fn, p);
182 /*_ , Footers */
184 Local variables:
185 c-file-style: "gnu"
186 mode: allout
187 End: