123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <faux/str.h>
- #include <faux/list.h>
- #include <faux/error.h>
- #include <klish/khelper.h>
- #include <klish/kplugin.h>
- #include <klish/kptype.h>
- #include <klish/kview.h>
- #include <klish/kscheme.h>
- #include <klish/kcontext.h>
- struct kscheme_s {
- faux_list_t *plugins;
- faux_list_t *ptypes;
- faux_list_t *views;
- };
- // Simple methods
- // PLUGIN list
- KGET(scheme, faux_list_t *, plugins);
- KCMP_NESTED(scheme, plugin, name);
- KCMP_NESTED_BY_KEY(scheme, plugin, name);
- KADD_NESTED(scheme, plugin);
- KFIND_NESTED(scheme, plugin);
- KNESTED_LEN(scheme, plugin);
- KNESTED_ITER(scheme, plugin);
- KNESTED_EACH(scheme, plugin);
- // PTYPE list
- KGET(scheme, faux_list_t *, ptypes);
- KCMP_NESTED(scheme, ptype, name);
- KCMP_NESTED_BY_KEY(scheme, ptype, name);
- KADD_NESTED(scheme, ptype);
- KFIND_NESTED(scheme, ptype);
- KNESTED_LEN(scheme, ptype);
- KNESTED_ITER(scheme, ptype);
- KNESTED_EACH(scheme, ptype);
- // VIEW list
- KGET(scheme, faux_list_t *, views);
- KCMP_NESTED(scheme, view, name);
- KCMP_NESTED_BY_KEY(scheme, view, name);
- KADD_NESTED(scheme, view);
- KFIND_NESTED(scheme, view);
- KNESTED_LEN(scheme, view);
- KNESTED_ITER(scheme, view);
- KNESTED_EACH(scheme, view);
- kscheme_t *kscheme_new(void)
- {
- kscheme_t *scheme = NULL;
- scheme = faux_zmalloc(sizeof(*scheme));
- assert(scheme);
- if (!scheme)
- return NULL;
- // PLUGIN list
- scheme->plugins = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
- kscheme_plugin_compare, kscheme_plugin_kcompare,
- (void (*)(void *))kplugin_free);
- assert(scheme->plugins);
- // PTYPE list
- scheme->ptypes = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
- kscheme_ptype_compare, kscheme_ptype_kcompare,
- (void (*)(void *))kptype_free);
- assert(scheme->ptypes);
- // VIEW list
- scheme->views = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
- kscheme_view_compare, kscheme_view_kcompare,
- (void (*)(void *))kview_free);
- assert(scheme->views);
- return scheme;
- }
- void kscheme_free(kscheme_t *scheme)
- {
- if (!scheme)
- return;
- faux_list_free(scheme->plugins);
- faux_list_free(scheme->ptypes);
- faux_list_free(scheme->views);
- faux_free(scheme);
- }
- #define TAG "PLUGIN"
- bool_t kscheme_load_plugins(kscheme_t *scheme, kcontext_t *context,
- faux_error_t *error)
- {
- bool_t retcode = BOOL_TRUE;
- kscheme_plugins_node_t *iter = NULL;
- kplugin_t *plugin = NULL;
- assert(scheme);
- if (!scheme)
- return BOOL_FALSE;
- assert(scheme->plugins);
- if (!context)
- return BOOL_FALSE;
- iter = kscheme_plugins_iter(scheme);
- while ((plugin = kscheme_plugins_each(&iter))) {
- int init_retcode = 0;
- if (!kplugin_load(plugin)) {
- faux_error_sprintf(error,
- TAG ": Can't load plugin \"%s\"",
- kplugin_name(plugin));
- retcode = BOOL_FALSE;
- continue; // Try to load all plugins
- }
- kcontext_set_type(context, KCONTEXT_PLUGIN_INIT);
- kcontext_set_plugin(context, plugin);
- if ((init_retcode = kplugin_init(plugin, context)) < 0) {
- faux_error_sprintf(error,
- TAG ": Can't init plugin \"%s\" (%d)",
- kplugin_name(plugin), init_retcode);
- retcode = BOOL_FALSE;
- continue;
- }
- }
- return retcode;
- }
- bool_t kscheme_fini_plugins(kscheme_t *scheme, kcontext_t *context,
- faux_error_t *error)
- {
- kscheme_plugins_node_t *iter = NULL;
- kplugin_t *plugin = NULL;
- assert(scheme);
- if (!scheme)
- return BOOL_FALSE;
- assert(scheme->plugins);
- if (!context)
- return BOOL_FALSE;
- iter = kscheme_plugins_iter(scheme);
- while ((plugin = kscheme_plugins_each(&iter))) {
- int fini_retcode = -1;
- kcontext_set_type(context, KCONTEXT_PLUGIN_FINI);
- kcontext_set_plugin(context, plugin);
- if ((fini_retcode = kplugin_fini(plugin, context)) < 0) {
- faux_error_sprintf(error,
- TAG ": Can't fini plugin \"%s\" (%d)",
- kplugin_name(plugin), fini_retcode);
- }
- }
- return BOOL_TRUE;
- }
- bool_t kscheme_fini(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
- {
- assert(scheme);
- if (!scheme)
- return BOOL_FALSE;
- if (!context)
- return BOOL_FALSE;
- if (!kscheme_fini_plugins(scheme, context, error))
- return BOOL_FALSE;
- return BOOL_TRUE;
- }
- ksym_t *kscheme_find_sym(const kscheme_t *scheme, const char *name)
- {
- char *saveptr = NULL;
- const char *delim = "@";
- char *plugin_name = NULL;
- char *cmd_name = NULL;
- char *full_name = NULL;
- ksym_t *sym = NULL;
- assert(scheme);
- if (!scheme)
- return NULL;
- assert(name);
- if (!name)
- return NULL;
- // Parse full name to get sym name and optional plugin name
- full_name = faux_str_dup(name);
- cmd_name = strtok_r(full_name, delim, &saveptr);
- if (!cmd_name) {
- faux_str_free(full_name);
- return NULL;
- }
- plugin_name = strtok_r(NULL, delim, &saveptr);
- // Search for symbol within specified PLUGIN only
- if (plugin_name) {
- kplugin_t *plugin = NULL;
- plugin = kscheme_find_plugin(scheme, plugin_name);
- if (!plugin)
- return NULL;
- sym = kplugin_find_sym(plugin, cmd_name);
- // Search for symbol within all PLUGINs
- } else {
- kscheme_plugins_node_t *iter = NULL;
- kplugin_t *plugin = NULL;
- iter = kscheme_plugins_iter(scheme);
- while ((plugin = kscheme_plugins_each(&iter))) {
- sym = kplugin_find_sym(plugin, cmd_name);
- if (sym)
- break;
- }
- }
- return sym;
- }
- bool_t kscheme_prepare_action_list(kscheme_t *scheme, faux_list_t *action_list,
- faux_error_t *error) {
- faux_list_node_t *iter = NULL;
- kaction_t *action = NULL;
- bool_t retcode = BOOL_TRUE;
- assert(scheme);
- if (!scheme)
- return BOOL_FALSE;
- assert(action_list);
- if (!action_list)
- return BOOL_FALSE;
- if (faux_list_is_empty(action_list))
- return BOOL_TRUE;
- iter = faux_list_head(action_list);
- while ((action = (kaction_t *)faux_list_each(&iter))) {
- ksym_t *sym = NULL;
- const char *sym_ref = kaction_sym_ref(action);
- sym = kscheme_find_sym(scheme, sym_ref);
- if (!sym) {
- faux_error_sprintf(error, "Can't find symbol \"%s\"",
- sym_ref);
- retcode = BOOL_FALSE;
- continue;
- }
- kaction_set_sym(action, sym);
- }
- return retcode;
- }
- bool_t kscheme_prepare_param_list(kscheme_t *scheme, faux_list_t *param_list,
- faux_error_t *error) {
- faux_list_node_t *iter = NULL;
- kparam_t *param = NULL;
- bool_t retcode = BOOL_TRUE;
- assert(scheme);
- if (!scheme)
- return BOOL_FALSE;
- assert(param_list);
- if (!param_list)
- return BOOL_FALSE;
- if (faux_list_is_empty(param_list))
- return BOOL_TRUE;
- iter = faux_list_head(param_list);
- while ((param = (kparam_t *)faux_list_each(&iter))) {
- kptype_t *ptype = NULL;
- const char *ptype_ref = kparam_ptype_ref(param);
- ptype = kscheme_find_ptype(scheme, ptype_ref);
- if (!ptype) {
- faux_error_sprintf(error, "Can't find ptype \"%s\"",
- ptype_ref);
- retcode = BOOL_FALSE;
- continue;
- }
- kparam_set_ptype(param, ptype);
- // Nested PARAMs
- if (!kscheme_prepare_param_list(scheme, kparam_params(param),
- error))
- retcode = BOOL_FALSE;
- }
- return retcode;
- }
- /** @brief Prepares schema for execution.
- *
- * It loads plugins, link unresolved symbols, then iterates all the
- * objects and link them to each other, check access
- * permissions. Without this function the schema is not fully functional.
- */
- bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
- {
- kscheme_views_node_t *views_iter = NULL;
- kview_t *view = NULL;
- assert(scheme);
- if (!scheme)
- return BOOL_FALSE;
- if (!context)
- return BOOL_FALSE;
- if (!kscheme_load_plugins(scheme, context, error))
- return BOOL_FALSE;
- // Iterate VIEWs
- views_iter = kscheme_views_iter(scheme);
- while ((view = kscheme_views_each(&views_iter))) {
- kview_commands_node_t *commands_iter = NULL;
- kcommand_t *command = NULL;
- printf("VIEW: %s\n", kview_name(view));
- // Iterate COMMANDs
- commands_iter = kview_commands_iter(view);
- while ((command = kview_commands_each(&commands_iter))) {
- // kview_commands_node_t *commands_iter = NULL;
- // kcommand_t *command = NULL;
- printf("COMMAND: %s\n", kcommand_name(command));
- // ACTIONs
- if (!kscheme_prepare_action_list(scheme,
- kcommand_actions(command), error))
- return BOOL_FALSE;
- // PARAMs
- if (!kscheme_prepare_param_list(scheme,
- kcommand_params(command), error))
- return BOOL_FALSE;
- }
- }
- #if 0
- clish_command_t *cmd;
- clish_view_t *view;
- clish_nspace_t *nspace;
- lub_list_t *view_tree, *nspace_tree;
- lub_list_node_t *nspace_iter, *view_iter;
- lub_bintree_t *cmd_tree;
- lub_bintree_iterator_t cmd_iter;
- clish_hook_access_fn_t *access_fn = NULL;
- clish_paramv_t *paramv;
- int i = 0;
- /* Iterate the VIEWs */
- view_tree = this->view_tree;
- view_iter = lub_list_iterator_init(view_tree);
- while(view_iter) {
- lub_list_node_t *old_view_iter;
- view = (clish_view_t *)lub_list_node__get_data(view_iter);
- old_view_iter = view_iter;
- view_iter = lub_list_node__get_next(view_iter);
- /* Check access rights for the VIEW */
- if (access_fn && clish_view__get_access(view) &&
- access_fn(this, clish_view__get_access(view))) {
- #ifdef DEBUG
- fprintf(stderr, "Warning: Access denied. Remove VIEW \"%s\"\n",
- clish_view__get_name(view));
- #endif
- lub_list_del(view_tree, old_view_iter);
- lub_list_node_free(old_view_iter);
- clish_view_delete(view);
- continue;
- }
- /* Iterate the NAMESPACEs */
- nspace_tree = clish_view__get_nspaces(view);
- nspace_iter = lub_list__get_head(nspace_tree);
- while(nspace_iter) {
- clish_view_t *ref_view;
- lub_list_node_t *old_nspace_iter;
- nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
- old_nspace_iter = nspace_iter;
- nspace_iter = lub_list_node__get_next(nspace_iter);
- /* Resolve NAMESPACEs and remove unresolved ones */
- ref_view = clish_shell_find_view(this, clish_nspace__get_view_name(nspace));
- if (!ref_view) {
- #ifdef DEBUG
- fprintf(stderr, "Warning: Remove unresolved NAMESPACE \"%s\" from \"%s\" VIEW\n",
- clish_nspace__get_view_name(nspace), clish_view__get_name(view));
- #endif
- lub_list_del(nspace_tree, old_nspace_iter);
- lub_list_node_free(old_nspace_iter);
- clish_nspace_delete(nspace);
- continue;
- }
- clish_nspace__set_view(nspace, ref_view);
- clish_nspace__set_view_name(nspace, NULL); /* Free some memory */
- /* Check access rights for the NAMESPACE */
- if (access_fn && (
- /* Check NAMESPASE owned access */
- (clish_nspace__get_access(nspace) && access_fn(this, clish_nspace__get_access(nspace)))
- ||
- /* Check referenced VIEW's access */
- (clish_view__get_access(ref_view) && access_fn(this, clish_view__get_access(ref_view)))
- )) {
- #ifdef DEBUG
- fprintf(stderr, "Warning: Access denied. Remove NAMESPACE \"%s\" from \"%s\" VIEW\n",
- clish_nspace__get_view_name(nspace), clish_view__get_name(view));
- #endif
- lub_list_del(nspace_tree, old_nspace_iter);
- lub_list_node_free(old_nspace_iter);
- clish_nspace_delete(nspace);
- continue;
- }
- }
- /* Iterate the COMMANDs */
- cmd_tree = clish_view__get_tree(view);
- cmd = lub_bintree_findfirst(cmd_tree);
- for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
- cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
- int cmd_is_alias = clish_command__get_alias(cmd)?1:0;
- clish_param_t *args = NULL;
- /* Check access rights for the COMMAND */
- if (access_fn && clish_command__get_access(cmd) &&
- access_fn(this, clish_command__get_access(cmd))) {
- #ifdef DEBUG
- fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
- clish_command__get_name(cmd), clish_view__get_name(view));
- #endif
- lub_bintree_remove(cmd_tree, cmd);
- clish_command_delete(cmd);
- continue;
- }
- /* Resolve command aliases */
- if (cmd_is_alias) {
- clish_view_t *aview;
- clish_command_t *cmdref;
- const char *alias_view = clish_command__get_alias_view(cmd);
- if (!alias_view)
- aview = clish_command__get_pview(cmd);
- else
- aview = clish_shell_find_view(this, alias_view);
- if (!aview /* Removed or broken VIEW */
- ||
- /* Removed or broken referenced COMMAND */
- !(cmdref = clish_view_find_command(aview, clish_command__get_alias(cmd), BOOL_FALSE))
- ) {
- #ifdef DEBUG
- fprintf(stderr, "Warning: Remove unresolved link \"%s\" from \"%s\" VIEW\n",
- clish_command__get_name(cmd), clish_view__get_name(view));
- #endif
- lub_bintree_remove(cmd_tree, cmd);
- clish_command_delete(cmd);
- continue;
- /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken VIEW for alias \"%s\"\n",
- clish_command__get_name(cmd));
- return -1; */
- /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias \"%s\"\n",
- clish_command__get_name(cmd));
- return -1; */
- }
- if (!clish_command_alias_to_link(cmd, cmdref)) {
- fprintf(stderr, CLISH_XML_ERROR_STR"Something wrong with alias \"%s\"\n",
- clish_command__get_name(cmd));
- return -1;
- }
- /* Check access rights for newly constructed COMMAND.
- Now the link has access filed from referenced command.
- */
- if (access_fn && clish_command__get_access(cmd) &&
- access_fn(this, clish_command__get_access(cmd))) {
- #ifdef DEBUG
- fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
- clish_command__get_name(cmd), clish_view__get_name(view));
- #endif
- lub_bintree_remove(cmd_tree, cmd);
- clish_command_delete(cmd);
- continue;
- }
- }
- if (cmd_is_alias) /* Don't duplicate paramv processing for aliases */
- continue;
- /* Iterate PARAMeters */
- paramv = clish_command__get_paramv(cmd);
- if (iterate_paramv(this, paramv, access_fn) < 0)
- return -1;
- /* Resolve PTYPE for args */
- if ((args = clish_command__get_args(cmd))) {
- if (!resolve_ptype(this, args))
- return -1;
- }
- }
- }
- #endif
- return BOOL_TRUE;
- }
|