diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2020-04-10 21:04:21 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2020-04-10 21:07:52 +0300 |
commit | a9da878ff22f980cea3bf3102113d7f2e65f61e9 (patch) | |
tree | dd3e21c580b91f6ab3d64e228ccb23170f3ddac3 /src/module.c | |
parent | da966c314f92d17b45ede9aa77ad3d3624c36725 (diff) | |
download | wydawca-a9da878ff22f980cea3bf3102113d7f2e65f61e9.tar.gz wydawca-a9da878ff22f980cea3bf3102113d7f2e65f61e9.tar.bz2 |
Change indentation to improve readability.
Diffstat (limited to 'src/module.c')
-rw-r--r-- | src/module.c | 429 |
1 files changed, 211 insertions, 218 deletions
diff --git a/src/module.c b/src/module.c index 1fa871d..22a9c6e 100644 --- a/src/module.c +++ b/src/module.c @@ -23,290 +23,283 @@ struct grecs_list *module_load_path, *module_prepend_load_path; static struct module * modlookup(const char *name) { - struct module *p; + struct module *p; - for (p = mod_head; p; p = p->next) - if (strcmp(p->name, name) == 0) - return p;; - return NULL; + for (p = mod_head; p; p = p->next) + if (strcmp(p->name, name) == 0) + return p;; + return NULL; } static struct module * -modinstall(const char *name, const char *path, grecs_locus_t *loc) +modinstall(const char *name, const char *path, grecs_locus_t * loc) { - struct module *p; - - p = modlookup(name); - if (p) { - grecs_error(loc, 0, _("module %s already declared"), name); - grecs_error(&p->locus, 0, _("previously declared here")); - return NULL; - } + struct module *p; - p = grecs_zalloc(sizeof(*p)); - p->name = grecs_strdup(name); - p->path = grecs_strdup(path); - p->locus.beg.file = grecs_strdup(loc->beg.file); - p->locus.beg.line = loc->beg.line; - p->locus.beg.col = loc->beg.col; - p->locus.end.file = grecs_strdup(loc->end.file); - p->locus.end.line = loc->end.line; - p->locus.end.col = loc->end.col; - - if (mod_tail) - mod_tail->next = p; - else - mod_head = p; - mod_tail = p; - - return p; + p = modlookup(name); + if (p) { + grecs_error(loc, 0, _("module %s already declared"), name); + grecs_error(&p->locus, 0, _("previously declared here")); + return NULL; + } + + p = grecs_zalloc(sizeof(*p)); + p->name = grecs_strdup(name); + p->path = grecs_strdup(path); + p->locus.beg.file = grecs_strdup(loc->beg.file); + p->locus.beg.line = loc->beg.line; + p->locus.beg.col = loc->beg.col; + p->locus.end.file = grecs_strdup(loc->end.file); + p->locus.end.line = loc->end.line; + p->locus.end.col = loc->end.col; + + if (mod_tail) + mod_tail->next = p; + else + mod_head = p; + mod_tail = p; + + return p; } int -cb_module(enum grecs_callback_command cmd, grecs_node_t *node, +cb_module(enum grecs_callback_command cmd, grecs_node_t * node, void *varptr, void *cb_data) { - grecs_value_t *name; - grecs_value_t *path; - grecs_locus_t *locus = &node->locus; - grecs_value_t *value = node->v.value; - - if (cmd != grecs_callback_set_value) { - grecs_error(locus, 0, _("Unexpected block statement")); - return 1; - } + grecs_value_t *name; + grecs_value_t *path; + grecs_locus_t *locus = &node->locus; + grecs_value_t *value = node->v.value; - if (!(name = get_arg(value, 0, GRECS_TYPE_STRING))) - return 1; - if (!(path = get_arg(value, 1, GRECS_TYPE_STRING))) - return 1; - - modinstall(name->v.string, path->v.string, locus); + if (cmd != grecs_callback_set_value) { + grecs_error(locus, 0, _("Unexpected block statement")); + return 1; + } - return 0; + if (!(name = get_arg(value, 0, GRECS_TYPE_STRING))) + return 1; + if (!(path = get_arg(value, 1, GRECS_TYPE_STRING))) + return 1; + + modinstall(name->v.string, path->v.string, locus); + + return 0; }; static void * resolve_sym(struct module *mod, const char *name, int mustbe) { - void *sym = lt_dlsym(mod->handle, name); - if (!sym && mustbe) { - grecs_error(&mod->locus, 0, - _("module \"%s\" does not define symbol \"%s\""), - mod->name, name); - } - return sym; + void *sym = lt_dlsym(mod->handle, name); + if (!sym && mustbe) { + grecs_error(&mod->locus, 0, + _("module \"%s\" does not define symbol \"%s\""), + mod->name, name); + } + return sym; } static int modload(struct module *mod, lt_dladvise advise) { - lt_dlhandle handle = NULL; - - if (mod->handle) { - grecs_error(&mod->locus, 0, _("already loaded")); - return 0; - } - - handle = lt_dlopenadvise(mod->path, advise); + lt_dlhandle handle = NULL; - if (!handle) { - grecs_error(mod->locus.beg.file ? &mod->locus : NULL, 0, - _("cannot load module %s: %s"), mod->path, - lt_dlerror()); - return 1; - } - mod->handle = handle; - mod->open = resolve_sym(mod, "wy_open", 0); - mod->close = resolve_sym(mod, "wy_close", 0); - - mod->config = resolve_sym(mod, "wy_config", 1); - mod->notify = resolve_sym(mod, "wy_notify", 1); - mod->flush = resolve_sym(mod, "wy_flush", 0); - - if (mod->open) { - if (mod->open(mod->modinit)) { - grecs_error(mod->modinit ? &mod->modinit->locus : NULL, - 0, - _("failed to initialize module %s"), - mod->name); - return 1; - } - } + if (mod->handle) { + grecs_error(&mod->locus, 0, _("already loaded")); return 0; + } + + handle = lt_dlopenadvise(mod->path, advise); + + if (!handle) { + grecs_error(mod->locus.beg.file ? &mod->locus : NULL, 0, + _("cannot load module %s: %s"), mod->path, + lt_dlerror()); + return 1; + } + mod->handle = handle; + mod->open = resolve_sym(mod, "wy_open", 0); + mod->close = resolve_sym(mod, "wy_close", 0); + + mod->config = resolve_sym(mod, "wy_config", 1); + mod->notify = resolve_sym(mod, "wy_notify", 1); + mod->flush = resolve_sym(mod, "wy_flush", 0); + + if (mod->open) { + if (mod->open(mod->modinit)) { + grecs_error(mod->modinit ? &mod->modinit->locus : NULL, + 0, _("failed to initialize module %s"), mod->name); + return 1; + } + } + return 0; } static int conf_notification_modules(struct notification *np) { - for (; np; np = np->next) { - if (np->modname) { - struct module *mod = modlookup(np->modname); - if (!mod) { - wy_log(LOG_ERR, "%s: no such module", - np->modname); - return 1; - } - if (!np->modcfg && mod->config && np->modnode) { - np->modcfg = mod->config(np->modnode); - if (!np->modcfg) { - wy_log(LOG_ERR, - "%s: failed to configure", - np->modname); - return 1; - } - } + for (; np; np = np->next) { + if (np->modname) { + struct module *mod = modlookup(np->modname); + if (!mod) { + wy_log(LOG_ERR, "%s: no such module", np->modname); + return 1; + } + if (!np->modcfg && mod->config && np->modnode) { + np->modcfg = mod->config(np->modnode); + if (!np->modcfg) { + wy_log(LOG_ERR, + "%s: failed to configure", np->modname); + return 1; } + } } - return 0; + } + return 0; } static int spoolmodcfg(struct spool *spool, void *unused) { - return conf_notification_modules(spool->notification); + return conf_notification_modules(spool->notification); } void modules_load() { - lt_dladvise advise = NULL; - struct grecs_list_entry *ep; - struct module *mod; - - if (lt_dlinit()) { - wy_log(LOG_ERR, _("failed to initialize libtool")); - return; - } - - /* Prepare load path */ - if (module_prepend_load_path) - for (ep = module_prepend_load_path->head; ep; ep = ep->next) - lt_dladdsearchdir(ep->data); - lt_dladdsearchdir(WYDAWCA_MODDIR); - if (module_load_path) - for (ep = module_load_path->head; ep; ep = ep->next) - lt_dladdsearchdir(ep->data); - - - if (lt_dladvise_init(&advise)) - wy_log(LOG_ERR, "lt_dladvise_init: %s", lt_dlerror()); - else { - if (lt_dladvise_ext(&advise)) - wy_log(LOG_ERR, "lt_dladvise_ext: %s", lt_dlerror()); - if (lt_dladvise_global(&advise)) - wy_log(LOG_ERR, "lt_dladvise_global: %s", - lt_dlerror()); - } - - for (mod = mod_head; mod; mod = mod->next) { - if (modload(mod, advise)) - exit(EX_UNAVAILABLE); - } - lt_dladvise_destroy(&advise); - - if (for_each_spool(spoolmodcfg, NULL)) { - wy_log(LOG_CRIT, - _("some modules failed to configure, exiting")); - exit(EX_UNAVAILABLE); - } - conf_notification_modules(default_notification); + lt_dladvise advise = NULL; + struct grecs_list_entry *ep; + struct module *mod; + + if (lt_dlinit()) { + wy_log(LOG_ERR, _("failed to initialize libtool")); + return; + } + + /* Prepare load path */ + if (module_prepend_load_path) + for (ep = module_prepend_load_path->head; ep; ep = ep->next) + lt_dladdsearchdir(ep->data); + lt_dladdsearchdir(WYDAWCA_MODDIR); + if (module_load_path) + for (ep = module_load_path->head; ep; ep = ep->next) + lt_dladdsearchdir(ep->data); + + + if (lt_dladvise_init(&advise)) + wy_log(LOG_ERR, "lt_dladvise_init: %s", lt_dlerror()); + else { + if (lt_dladvise_ext(&advise)) + wy_log(LOG_ERR, "lt_dladvise_ext: %s", lt_dlerror()); + if (lt_dladvise_global(&advise)) + wy_log(LOG_ERR, "lt_dladvise_global: %s", lt_dlerror()); + } + + for (mod = mod_head; mod; mod = mod->next) { + if (modload(mod, advise)) + exit(EX_UNAVAILABLE); + } + lt_dladvise_destroy(&advise); + + if (for_each_spool(spoolmodcfg, NULL)) { + wy_log(LOG_CRIT, _("some modules failed to configure, exiting")); + exit(EX_UNAVAILABLE); + } + conf_notification_modules(default_notification); } void modules_close() { - struct module *mod; - - for (mod = mod_head; mod; mod = mod->next) { - if (mod->close) - mod->close(); - lt_dlclose(mod->handle); - } + struct module *mod; + + for (mod = mod_head; mod; mod = mod->next) { + if (mod->close) + mod->close(); + lt_dlclose(mod->handle); + } } int -module_set_init(const char *name, grecs_node_t *node) +module_set_init(const char *name, grecs_node_t * node) { - struct module *mod = modlookup(name); - if (!mod) - return 1; - mod->modinit = node; - return 0; + struct module *mod = modlookup(name); + if (!mod) + return 1; + mod->modinit = node; + return 0; } - + void module_notify(const char *name, void *modcfg, enum wy_event ev, struct wy_triplet *trp) { - struct module *mod = modlookup(name); - - if (!mod) { - wy_log(LOG_ERR, "no such module: %s", name); - return; - } - if (mod->notify) - mod->notify(modcfg, ev, trp); + struct module *mod = modlookup(name); + + if (!mod) { + wy_log(LOG_ERR, "no such module: %s", name); + return; + } + if (mod->notify) + mod->notify(modcfg, ev, trp); } void module_flush(const char *name, void *modcfg) { - struct module *mod = modlookup(name); - - if (!mod) { - wy_log(LOG_ERR, "no such module: %s", name); - return; - } - if (mod->flush) - mod->flush(modcfg); + struct module *mod = modlookup(name); + + if (!mod) { + wy_log(LOG_ERR, "no such module: %s", name); + return; + } + if (mod->flush) + mod->flush(modcfg); } void module_help(const char *modname) { - lt_dladvise advise = NULL; - struct module mod; - void (*help)(void); - - if (lt_dlinit()) { - wy_log(LOG_ERR, _("failed to initialize libtool")); - exit(EX_UNAVAILABLE); - } + lt_dladvise advise = NULL; + struct module mod; + void (*help) (void); + + if (lt_dlinit()) { + wy_log(LOG_ERR, _("failed to initialize libtool")); + exit(EX_UNAVAILABLE); + } + + if (strchr(modname, '/')) { + char const *basename; + char *dirname; + + basename = split_filename(modname, &dirname); + lt_dladdsearchdir(dirname); + modname = basename; + } else + lt_dladdsearchdir(WYDAWCA_MODDIR); - if (strchr(modname, '/')) { - char const *basename; - char *dirname; - - basename = split_filename(modname, &dirname); - lt_dladdsearchdir(dirname); - modname = basename; - } else - lt_dladdsearchdir(WYDAWCA_MODDIR); - - if (lt_dladvise_init(&advise)) - wy_log(LOG_ERR, "lt_dladvise_init: %s", lt_dlerror()); - else { - if (lt_dladvise_ext(&advise)) - wy_log(LOG_ERR, "lt_dladvise_ext: %s", lt_dlerror()); - if (lt_dladvise_global(&advise)) - wy_log(LOG_ERR, "lt_dladvise_global: %s", - lt_dlerror()); - } + if (lt_dladvise_init(&advise)) + wy_log(LOG_ERR, "lt_dladvise_init: %s", lt_dlerror()); + else { + if (lt_dladvise_ext(&advise)) + wy_log(LOG_ERR, "lt_dladvise_ext: %s", lt_dlerror()); + if (lt_dladvise_global(&advise)) + wy_log(LOG_ERR, "lt_dladvise_global: %s", lt_dlerror()); + } + + memset(&mod, 0, sizeof(mod)); + mod.path = (char *) modname; + + if (modload(&mod, advise)) + exit(EX_UNAVAILABLE); + + lt_dladvise_destroy(&advise); + + help = resolve_sym(&mod, "wy_help", 0); + if (!help) + wy_log(LOG_NOTICE, "no help for %s", modname); + else + help(); - memset(&mod, 0, sizeof(mod)); - mod.path = (char*) modname; - - if (modload(&mod, advise)) - exit(EX_UNAVAILABLE); - - lt_dladvise_destroy(&advise); - - help = resolve_sym(&mod, "wy_help", 0); - if (!help) - wy_log(LOG_NOTICE, "no help for %s", modname); - else - help(); - - lt_dlclose(mod.handle); + lt_dlclose(mod.handle); } |