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
|
/* mod.c: loadable nawm modules */
/* Copyright (C) 1999 by the Massachusetts Institute of Technology.
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting
* documentation, and that the name of M.I.T. not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is"
* without express or implied warranty.
*/
#include "nawm.h"
#include "lang.h"
#include "parser.h"
#include <dlfcn.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef NEED_UNDERSCORES
#define SYMNAME(x) ("_" x)
#else
#define SYMNAME
#endif
static char **nawm_libpath;
extern long eventmask;
extern event_handler *eventhandlers;
void initmodules(void)
{
char *libpath, *p;
int i;
libpath = getenv("NAWM_LIBRARY_PATH");
if (!libpath)
libpath = NAWM_DEFAULT_LIBPATH;
for (i = 0, p = libpath; *p; p++)
{
if (*p == ':')
i++;
}
nawm_libpath = xmalloc((i + 2) * sizeof(char *));
/* split libpath into components, recognizing an empty component
* as NAWM_DEFAULT_LIBPATH
*/
for (i = 0, p = libpath; *libpath; p++)
{
if (*p == ':' || *p == '\0')
{
if (p == libpath)
nawm_libpath[i] = NAWM_DEFAULT_LIBPATH;
else
{
int len = p - libpath;
while (libpath[len] == '/')
len--;
nawm_libpath[i] = xmalloc(len + 1);
memcpy(nawm_libpath[i], libpath, len);
nawm_libpath[i][len] = '\0';
}
libpath = *p ? p + 1 : p;
i++;
}
}
nawm_libpath[i] = NULL;
}
void load_module(char *name)
{
int i, len = strlen(name), *modrev, found = 0, opened = 0;
void *dlconfig = NULL;
void (*modinit)();
for (i = 0; nawm_libpath[i]; i++)
{
char *path = xmalloc(strlen(nawm_libpath[i]) + len + 5);
sprintf(path, "%s/%s.so", nawm_libpath[i], name);
if (!access(path, R_OK))
{
found = 1;
dlconfig = dlopen(path, RTLD_NOW);
free(path);
if (!dlconfig)
continue;
opened = 1;
modrev = dlsym(dlconfig, SYMNAME("nawm_modrev"));
if (!modrev || *modrev != 1)
continue;
modinit = dlsym(dlconfig, SYMNAME("modinit"));
if (!modinit)
continue;
modinit();
return;
}
}
if (opened)
{
if (modrev)
die("No symbol \"modinit\" in module \"%s\".", name);
else
die("No symbol \"nawm_modrev\" in module \"%s\".", name);
}
else if (found)
die("Couldn't open module \"%s\":\n%s", name, dlerror());
else
die("Couldn't find module \"%s\"", name);
}
void module_define_procedure(char *name, dtype rtype, void (*body)(),
int numargs, ...)
{
function *f;
va_list ap;
int i, nargs;
nargs = (numargs < 0) ? -numargs : numargs;
f = xmalloc(sizeof(function) + (nargs - 3) * sizeof(dtype));
f->body = body;
f->type = rtype;
f->numvars = -1;
f->numargs = numargs;
if (numargs < 0)
numargs = -numargs;
va_start(ap, numargs);
for (i = 0; i < numargs; i++)
f->vartype[i] = va_arg(ap, dtype);
va_end(ap);
define(rtype ? FUN : CMD, name, f);
}
void module_define_variable(char *name, dtype type, nawmval data)
{
variable *v = mkvar(type);
v->data = data;
define(VAR, name, v);
}
int module_define_type(char *name)
{
return newtype(name);
}
int module_lookup_type(char *name)
{
int kind;
int type = (nawmval)lookup(name, &kind, 0);
if (kind != DTYPE || type == 0)
return -1;
else
return type;
}
void module_define_event_handler(void (*handler)(XEvent *), long mask)
{
event_handler *h;
eventmask |= mask;
h = xmalloc(sizeof(event_handler));
h->handler = handler;
h->next = eventhandlers;
eventhandlers = h;
}
|