2
Copyright (c) 2003-2008 uim Project http://code.google.com/p/uim/
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
1. Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
12
2. Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in the
14
documentation and/or other materials provided with the distribution.
15
3. Neither the name of authors nor the names of its contributors
16
may be used to endorse or promote products derived from this software
17
without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
20
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
23
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42
#include <sys/types.h>
50
#include "uim-scm-abbrev.h"
51
#include "uim-internal.h"
52
#include "uim-notify.h"
55
#define NOTIFY_PLUGIN_PATH LIBDIR "/uim/notify"
56
#define NOTIFY_PLUGIN_PREFIX "libuimnotify-"
57
#define NOTIFY_PLUGIN_SUFFIX ".so"
60
typedef void (*my_dlfunc_t)(void);
61
#define dlfunc(handle, symbol) \
62
((my_dlfunc_t)(uintptr_t)dlsym((handle), (symbol)))
64
typedef dlfunc_t my_dlfunc_t;
67
struct uim_notify_agent {
68
const uim_notify_desc *(*desc)(void);
71
int (*notify_info)(const char *);
72
int (*notify_fatal)(const char *);
75
static my_dlfunc_t load_func(const char *path, const char *name);
77
/* FIXME: Move these decls to the 'stderr' agent section and make
78
* invisible from other part of uim-notify. -- YamaKen 2008-01-30 */
79
static void uim_notify_load_stderr(void);
80
static const uim_notify_desc *uim_notify_stderr_get_desc(void);
83
static struct uim_notify_agent agent_body;
84
static struct uim_notify_agent *agent = &agent_body;
85
static void *notify_dlhandle = NULL;
89
load_func(const char *path, const char *name)
93
f = (my_dlfunc_t)dlfunc(notify_dlhandle, name);
95
fprintf(stderr, "uim-notify: cannot found '%s()' in %s\n", name, path);
96
dlclose(notify_dlhandle);
97
uim_notify_load_stderr();
104
uim_notify_load(const char *name)
106
if (!agent->quit || !agent->desc) {
107
fprintf(stderr, "uim-notify: notification agent module is not loaded\n");
108
uim_notify_load_stderr();
112
if (strcmp(agent->desc()->name, name) == 0) {
114
} else if (strcmp(name, "stderr") == 0) {
117
dlclose(notify_dlhandle);
118
uim_notify_load_stderr();
125
dlclose(notify_dlhandle);
127
snprintf(path, sizeof(path), "%s/%s%s%s", NOTIFY_PLUGIN_PATH,
128
NOTIFY_PLUGIN_PREFIX, name, NOTIFY_PLUGIN_SUFFIX);
130
notify_dlhandle = dlopen(path, RTLD_NOW);
131
if ((str = dlerror())) {
132
fprintf(stderr, "uim-notify: load failed %s(%s)\n", path, str);
133
uim_notify_load_stderr();
136
agent->desc = (const uim_notify_desc *(*)(void))load_func(path, "uim_notify_plugin_get_desc");
140
agent->init = (int (*)(void))load_func(path, "uim_notify_plugin_init");
144
agent->quit = (void (*)(void))load_func(path, "uim_notify_plugin_quit");
148
agent->notify_info = (int (*)(const char *))load_func(path, "uim_notify_plugin_info");
149
if (!agent->notify_info)
152
agent->notify_fatal = (int (*)(const char *))load_func(path, "uim_notify_plugin_fatal");
153
if (!agent->notify_fatal)
161
const uim_notify_desc *
162
uim_notify_get_desc(void)
164
return agent->desc();
168
uim_notify_init(void)
170
/* Since a cyclic init/quit sequence leaves *agent uncleared,
171
* explicit initialization is required. Such data initialization is
172
* needed for some embedded platforms such as Qtopia.
173
* -- YamaKen 2008-01-30 */
174
uim_notify_load_stderr();
176
return agent->init();
180
uim_notify_quit(void)
186
uim_notify_info(const char *msg_fmt, ...)
191
va_start(ap, msg_fmt);
192
vsnprintf(msg, sizeof(msg), msg_fmt, ap);
195
return agent->notify_info(msg);
199
uim_notify_fatal(const char *msg_fmt, ...)
204
va_start(ap, msg_fmt);
205
vsnprintf(msg, sizeof(msg), msg_fmt, ap);
208
return agent->notify_fatal(msg);
211
/* Low stack-consumption version of uim_notify_fatal(). */
213
uim_notify_fatal_raw(const char *msg)
215
return agent->notify_fatal(msg);
222
notify_get_plugins(void)
228
const uim_notify_desc *desc;
230
uim_notify_desc *(*desc_func)(void);
233
plen = sizeof(NOTIFY_PLUGIN_PREFIX);
234
slen = sizeof(NOTIFY_PLUGIN_SUFFIX);
236
desc = uim_notify_stderr_get_desc();
237
ret_ = CONS(LIST3(MAKE_SYM(desc->name),
238
MAKE_STR(desc->name),
239
MAKE_STR(desc->desc)),
241
dirp = opendir(NOTIFY_PLUGIN_PATH);
243
while ((dp = readdir(dirp)) != NULL) {
244
size_t len = strlen(dp->d_name);
246
if ((len < plen + slen - 1) ||
247
(PATH_MAX < (sizeof(NOTIFY_PLUGIN_PATH "/") + len)) ||
248
(strcmp(dp->d_name, NOTIFY_PLUGIN_PREFIX) <= 0) ||
249
(strcmp(dp->d_name + len + 1 - slen, NOTIFY_PLUGIN_SUFFIX) != 0))
252
snprintf(path, sizeof(path), "%s/%s", NOTIFY_PLUGIN_PATH, dp->d_name);
253
handle = dlopen(path, RTLD_NOW);
254
if ((str = dlerror()) != NULL) {
255
fprintf(stderr, "load failed %s(%s)\n", path, str);
258
desc_func = (uim_notify_desc *(*)(void))dlfunc(handle, "uim_notify_plugin_get_desc");
260
fprintf(stderr, "cannot found 'uim_notify_get_desc()' in %s\n", path);
267
ret_ = CONS(LIST3(MAKE_SYM(desc->name),
268
MAKE_STR(desc->name),
269
MAKE_STR(desc->desc)),
274
(void)closedir(dirp);
276
return uim_scm_callf("reverse", "o", ret_);
280
notify_load(uim_lisp name_)
282
const char *name = REFER_C_STR(name_);
284
return MAKE_BOOL(uim_notify_load(name));
288
notify_info(uim_lisp msg_)
290
const char *msg = REFER_C_STR(msg_);
292
return MAKE_BOOL(uim_notify_info("%s", msg));
296
notify_fatal(uim_lisp msg_)
298
const char *msg = REFER_C_STR(msg_);
300
return MAKE_BOOL(uim_notify_fatal("%s", msg));
304
uim_init_notify_subrs(void)
306
uim_scm_init_proc0("uim-notify-get-plugins", notify_get_plugins);
307
uim_scm_init_proc1("uim-notify-load", notify_load);
308
uim_scm_init_proc1("uim-notify-info", notify_info);
309
uim_scm_init_proc1("uim-notify-fatal", notify_fatal);
314
* builtin 'stderr' notification agent
316
static uim_bool uim_notify_stderr_init(void);
317
static void uim_notify_stderr_quit(void);
318
static uim_bool uim_notify_stderr_info(const char *);
319
static uim_bool uim_notify_stderr_fatal(const char *);
321
static const uim_notify_desc uim_notify_stderr_desc = {
323
"Standard Error output",
327
uim_notify_load_stderr(void)
329
agent->desc = uim_notify_stderr_get_desc;
330
agent->init = uim_notify_stderr_init;
331
agent->quit = uim_notify_stderr_quit;
332
agent->notify_info = uim_notify_stderr_info;
333
agent->notify_fatal = uim_notify_stderr_fatal;
334
notify_dlhandle = NULL;
337
static const uim_notify_desc *
338
uim_notify_stderr_get_desc(void)
340
return &uim_notify_stderr_desc;
344
uim_notify_stderr_init(void)
350
uim_notify_stderr_quit(void)
356
uim_notify_stderr_info(const char *msg)
358
fputs("libuim: [info] ", stderr);
366
uim_notify_stderr_fatal(const char *msg)
368
/* To reduce stack consumption on hard situations such as memory
369
* exhaustion, printf()s with indirect directives are intentionally
370
* avoided here. -- YamaKen 2008-02-11 */
371
fputs("libuim: [fatal] ", stderr);