diff options
Diffstat (limited to 'other/b-scan/tmp/src/module.c')
| -rw-r--r-- | other/b-scan/tmp/src/module.c | 214 |
1 files changed, 214 insertions, 0 deletions
diff --git a/other/b-scan/tmp/src/module.c b/other/b-scan/tmp/src/module.c new file mode 100644 index 0000000..1748914 --- /dev/null +++ b/other/b-scan/tmp/src/module.c | |||
| @@ -0,0 +1,214 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <dlfcn.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <bscan/module.h> | ||
| 6 | #include <bscan/system.h> | ||
| 7 | |||
| 8 | struct _mods mods[MAX_MODULES]; | ||
| 9 | /* modstructures not initialized */ | ||
| 10 | int modcount = -1; | ||
| 11 | |||
| 12 | /* | ||
| 13 | #ifdef HAVE_DLSYM | ||
| 14 | #define MAKE_DLSYM(x, y) mods[modcount].##x = dlsym(handle, ##y);\ | ||
| 15 | if ((error = dlerror()) != NULL) {\ | ||
| 16 | fprintf(stderr, ##y":%s\n", error); return(1); } | ||
| 17 | #endif | ||
| 18 | */ | ||
| 19 | |||
| 20 | /* I think this is more correct, and also gets rid of some compile warnings. | ||
| 21 | * hope this doesn't break anything. -typo */ | ||
| 22 | #ifdef HAVE_DLSYM | ||
| 23 | #define MAKE_DLSYM(x, y) mods[modcount].x = dlsym(handle, y);\ | ||
| 24 | if ((error = dlerror()) != NULL) {\ | ||
| 25 | fprintf(stderr, y":%s\n", error); return(1); } | ||
| 26 | #endif | ||
| 27 | |||
| 28 | /* SunOS 4.1.3 support */ | ||
| 29 | #ifndef RTLD_NOW | ||
| 30 | #define RTLD_NOW 0x00001 | ||
| 31 | #endif | ||
| 32 | /* OpenBSD support */ | ||
| 33 | #ifndef RTLD_GLOBAL | ||
| 34 | #define RTLD_GLOBAL 0x00000 | ||
| 35 | #endif | ||
| 36 | |||
| 37 | /* we really hate theo for this shit of dl* work */ | ||
| 38 | #if defined(__OpenBSD__) | ||
| 39 | # if !(defined(__mips) || defined(__powerpc)) | ||
| 40 | # define DLSYM_AOUT 1 | ||
| 41 | # else | ||
| 42 | # define DLSYM_AOUT 0 | ||
| 43 | # endif | ||
| 44 | #endif | ||
| 45 | #if DLSYM_AOUT == 1 | ||
| 46 | # define DLSYM_UNDERSCORE "_" | ||
| 47 | #else | ||
| 48 | # define DLSYM_UNDERSCORE /**/ | ||
| 49 | #endif | ||
| 50 | |||
| 51 | |||
| 52 | /* | ||
| 53 | * init the module structures. NOT the modules! | ||
| 54 | */ | ||
| 55 | void | ||
| 56 | init_modules () | ||
| 57 | { | ||
| 58 | int c = 0; | ||
| 59 | |||
| 60 | while (c < MAX_MODULES) | ||
| 61 | { | ||
| 62 | mods[c].init = NULL; | ||
| 63 | mods[c].fini = NULL; | ||
| 64 | mods[c].musage = NULL; | ||
| 65 | mods[c].modname = NULL; | ||
| 66 | mods[c].modid = 0; | ||
| 67 | mods[c].modarg = NULL; | ||
| 68 | mods[c++].callmdl = NULL; | ||
| 69 | } | ||
| 70 | modcount = 0; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | /* | ||
| 75 | * Load a module | ||
| 76 | * Return 0 on success, != 0 on error [no space left, EACCESS, ...] | ||
| 77 | */ | ||
| 78 | int | ||
| 79 | add_module (char *fname, char *modarg) | ||
| 80 | { | ||
| 81 | #ifdef HAVE_DLSYM | ||
| 82 | void *handle; | ||
| 83 | char *error; | ||
| 84 | |||
| 85 | if (modcount == -1) | ||
| 86 | init_modules (); | ||
| 87 | |||
| 88 | if (modcount >= MAX_MODULES) | ||
| 89 | return (2); /* array to small! */ | ||
| 90 | |||
| 91 | handle = dlopen (fname, RTLD_NOW | RTLD_GLOBAL); | ||
| 92 | |||
| 93 | if ((error = dlerror ()) != NULL) | ||
| 94 | { | ||
| 95 | fprintf (stderr, "%s\n", error); | ||
| 96 | return (1); | ||
| 97 | } | ||
| 98 | |||
| 99 | MAKE_DLSYM (init, DLSYM_UNDERSCORE"init"); | ||
| 100 | MAKE_DLSYM (fini, DLSYM_UNDERSCORE"fini"); | ||
| 101 | MAKE_DLSYM (musage, DLSYM_UNDERSCORE"musage"); | ||
| 102 | MAKE_DLSYM (callmdl, DLSYM_UNDERSCORE"callmdl"); | ||
| 103 | |||
| 104 | mods[modcount].modid = modcount; | ||
| 105 | mods[modcount].modarg = modarg; /* not encoded arg */ | ||
| 106 | |||
| 107 | modcount++; | ||
| 108 | |||
| 109 | #endif | ||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | /* | ||
| 114 | * split a 'space seperated string' into many arguements | ||
| 115 | * decode esc-sequences | ||
| 116 | */ | ||
| 117 | void | ||
| 118 | split_margs (const char *moptarg, char ***margvp, int *margcp) | ||
| 119 | { | ||
| 120 | char *ptr, *opt; | ||
| 121 | int off; | ||
| 122 | char ch, ch2; | ||
| 123 | |||
| 124 | if (margcp == NULL) | ||
| 125 | return; | ||
| 126 | |||
| 127 | if (margvp == NULL) | ||
| 128 | return; | ||
| 129 | |||
| 130 | if (moptarg == NULL) | ||
| 131 | return; | ||
| 132 | |||
| 133 | moptarg = strdup(moptarg); | ||
| 134 | |||
| 135 | /* | ||
| 136 | * convert " modname -a arg1 -b arg2" to | ||
| 137 | * "modname -a arg1 -b arg2" | ||
| 138 | */ | ||
| 139 | opt = (char *) calloc (1, strlen (moptarg) + 1); | ||
| 140 | off = 0; | ||
| 141 | ch2 = ' '; | ||
| 142 | ptr = (char *) moptarg; | ||
| 143 | while ((ch = *ptr++) != '\0') | ||
| 144 | { | ||
| 145 | if ((ch == ' ') && (ch2 == ' ')) | ||
| 146 | continue; | ||
| 147 | opt[off++] = ch; | ||
| 148 | ch2 = ch; | ||
| 149 | } | ||
| 150 | if (ch2 == ' ') | ||
| 151 | opt[off - 1] = '\0'; | ||
| 152 | |||
| 153 | /* | ||
| 154 | * split argument-string into char *argv[] array | ||
| 155 | */ | ||
| 156 | *margcp = 0; | ||
| 157 | while ((ptr = strchr (opt, ' ')) != NULL) | ||
| 158 | { | ||
| 159 | *ptr++ = '\0'; | ||
| 160 | |||
| 161 | (*margvp) = realloc (*margvp, ((*margcp) + 1) * sizeof (char *)); | ||
| 162 | |||
| 163 | ctoreal(opt, opt); /* decode esc-sequences */ | ||
| 164 | *(*margvp + *margcp) = opt; | ||
| 165 | (*margcp)++; | ||
| 166 | opt = ptr; | ||
| 167 | } | ||
| 168 | (*margvp) = realloc (*margvp, ((*margcp) + 2) * sizeof (char *)); | ||
| 169 | ctoreal(opt, opt); | ||
| 170 | *(*margvp + (*margcp)++) = opt; | ||
| 171 | *(*margvp + (*margcp)) = NULL; /* terminate the array */ | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | /* | ||
| 176 | * load and init the module. | ||
| 177 | * this function can exit | ||
| 178 | * return 0 on success | ||
| 179 | */ | ||
| 180 | int | ||
| 181 | loadinit_mod(char *optar) | ||
| 182 | { | ||
| 183 | char **margv = NULL; | ||
| 184 | int margc = 0; | ||
| 185 | extern int optind; | ||
| 186 | extern struct _opt *opt; | ||
| 187 | |||
| 188 | split_margs (optar, &margv, &margc); | ||
| 189 | if (add_module (margv[0], optar) == 0) | ||
| 190 | { | ||
| 191 | int oldoptind = optind; | ||
| 192 | int m = modcount - 1; | ||
| 193 | optind = 1; | ||
| 194 | |||
| 195 | if (mods[m].init ((char **) &mods[m].modname, margc, margv, opt) != 0) | ||
| 196 | { | ||
| 197 | fprintf (stderr, "- [%d]: '%s' init FAILED\n", | ||
| 198 | mods[m].modid, margv[0]); | ||
| 199 | mods[m].musage (); | ||
| 200 | exit (-1); | ||
| 201 | } else | ||
| 202 | fprintf (stderr, "+ [%d]: '%s' initialized\n", | ||
| 203 | mods[m].modid, mods[m].modname); | ||
| 204 | optind = oldoptind; /* restore old optind value */ | ||
| 205 | } else | ||
| 206 | { | ||
| 207 | fprintf (stderr, "+ [-]; '%s' failed\n", optar); | ||
| 208 | exit (-1); | ||
| 209 | } | ||
| 210 | |||
| 211 | return(0); | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
