123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- /*
- * nspace.c
- *
- * This file provides the implementation of the "nspace" class
- */
- #include "private.h"
- #include "lub/string.h"
- #include "lub/ctype.h"
- #include <assert.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <regex.h>
- #include <ctype.h>
- /*--------------------------------------------------------- */
- static void clish_nspace_init(clish_nspace_t *this, const char *view_name)
- {
- this->view_name = NULL;
- clish_nspace__set_view_name(this, view_name);
- /* Set up defaults */
- this->view = NULL;
- this->prefix = NULL;
- this->help = BOOL_FALSE;
- this->completion = BOOL_TRUE;
- this->context_help = BOOL_FALSE;
- this->inherit = BOOL_TRUE;
- this->prefix_cmd = NULL;
- this->access = NULL;
- this->cmds = lub_list_new(clish_command_compare, clish_command_delete);
- }
- /*--------------------------------------------------------- */
- static void clish_nspace_fini(clish_nspace_t *this)
- {
- /* Deallocate the memory for this instance */
- if (this->prefix) {
- free(this->prefix);
- regfree(&this->prefix_regex);
- }
- /* Delete COMMANDs */
- lub_list_free_all(this->cmds);
- /* Delete prefix pseudo-command */
- if (this->prefix_cmd) {
- clish_command_delete(this->prefix_cmd);
- this->prefix_cmd = NULL;
- }
- lub_string_free(this->access);
- lub_string_free(this->view_name);
- }
- /*--------------------------------------------------------- */
- clish_command_t * clish_nspace_create_prefix_cmd(clish_nspace_t * this,
- const char * name, const char * help)
- {
- if (this->prefix_cmd) {
- clish_command_delete(this->prefix_cmd);
- this->prefix_cmd = NULL;
- }
- return (this->prefix_cmd = clish_command_new(name, help));
- }
- /*--------------------------------------------------------- */
- static clish_command_t *clish_nspace_find_create_command(clish_nspace_t * this,
- const char *prefix, const clish_command_t * ref)
- {
- clish_command_t *cmd;
- char *name = NULL;
- const char *help = NULL;
- clish_command_t *tmp = NULL;
- const char *str = NULL;
- lub_list_node_t *iter = NULL;
- assert(prefix);
- if (!ref) {
- assert(this->prefix_cmd);
- name = lub_string_dup(prefix);
- ref = this->prefix_cmd;
- help = clish_command__get_text(this->prefix_cmd);
- } else {
- lub_string_catn(&name, prefix, strlen(prefix));
- lub_string_catn(&name, " ", 1);
- lub_string_catn(&name, clish_command__get_name(ref),
- strlen(clish_command__get_name(ref)));
- help = clish_command__get_text(ref);
- }
- /* The command is cached already */
- if ((cmd = lub_list_find(this->cmds, clish_command_fn_find_by_name, name))) {
- free(name);
- return cmd;
- }
- cmd = clish_command_new_link(name, help, ref);
- free(name);
- assert(cmd);
- /* The command was created dynamically */
- clish_command__set_dynamic(cmd, BOOL_TRUE);
- /* Delete proxy commands with another prefixes */
- iter = lub_list__get_head(this->cmds);
- if (iter)
- tmp = lub_list_node__get_data(iter);
- if (tmp)
- str = clish_command__get_name(tmp);
- if (str && (lub_string_nocasestr(str, prefix) != str)) {
- do {
- clish_command_delete(tmp);
- lub_list_del(this->cmds, iter);
- lub_list_node_free(iter);
- iter = lub_list__get_head(this->cmds);
- if (iter)
- tmp = lub_list_node__get_data(iter);
- } while (iter);
- }
- /* Insert command link into the tree */
- if (!lub_list_add(this->cmds, cmd)) {
- clish_command_delete(cmd);
- cmd = NULL;
- }
- return cmd;
- }
- /*--------------------------------------------------------- */
- clish_nspace_t *clish_nspace_new(const char *view_name)
- {
- clish_nspace_t *this = malloc(sizeof(clish_nspace_t));
- if (this)
- clish_nspace_init(this, view_name);
- return this;
- }
- /*--------------------------------------------------------- */
- void clish_nspace_delete(void *data)
- {
- clish_nspace_t *this = (clish_nspace_t *)data;
- clish_nspace_fini(this);
- free(this);
- }
- /*--------------------------------------------------------- */
- static const char *clish_nspace_after_prefix(const regex_t *prefix_regex,
- const char *line, char **real_prefix)
- {
- const char *in_line = NULL;
- regmatch_t pmatch[1];
- int res;
- if (!line)
- return NULL;
- /* Compile regular expression */
- res = regexec(prefix_regex, line, 1, pmatch, 0);
- if (res || (0 != pmatch[0].rm_so))
- return NULL;
- /* Empty match */
- if (0 == pmatch[0].rm_eo)
- return NULL;
- in_line = line + pmatch[0].rm_eo;
- lub_string_catn(real_prefix, line, pmatch[0].rm_eo);
- return in_line;
- }
- /*--------------------------------------------------------- */
- clish_command_t *clish_nspace_resolve_prefix(clish_nspace_t *this, const char *name)
- {
- clish_command_t *cmd = NULL, *retval = NULL;
- clish_view_t *view = clish_nspace__get_view(this);
- const char *in_line;
- char *real_prefix = NULL;
- if (!clish_nspace__get_prefix(this))
- return clish_view_resolve_prefix(view, name);
- if (!(in_line = clish_nspace_after_prefix(
- clish_nspace__get_prefix_regex(this), name, &real_prefix)))
- return NULL;
- /* If prefix is followed by space */
- while (lub_ctype_isspace(in_line[0]))
- in_line++;
- if (in_line[0] != '\0') {
- cmd = clish_view_resolve_prefix(view, in_line);
- if (!cmd) {
- lub_string_free(real_prefix);
- return NULL;
- }
- }
- retval = clish_nspace_find_create_command(this, real_prefix, cmd);
- lub_string_free(real_prefix);
- return retval;
- }
- /*--------------------------------------------------------- */
- const clish_command_t *clish_nspace_find_next_completion(clish_nspace_t * this,
- const char *iter_cmd, const char *line,
- clish_nspace_visibility_e field)
- {
- const clish_command_t *cmd = NULL, *retval = NULL;
- clish_view_t *view = clish_nspace__get_view(this);
- const char *in_iter = "";
- const char *in_line;
- char *real_prefix = NULL;
- if (!clish_nspace__get_prefix(this))
- return clish_view_find_next_completion(view, iter_cmd,
- line, field, this->inherit);
- if (!(in_line = clish_nspace_after_prefix(
- clish_nspace__get_prefix_regex(this), line, &real_prefix)))
- return NULL;
- if (in_line[0] != '\0') {
- /* If prefix is followed by space */
- if (!isspace(in_line[0])) {
- lub_string_free(real_prefix);
- return NULL;
- }
- in_line++;
- if (iter_cmd &&
- (lub_string_nocasestr(iter_cmd, real_prefix) == iter_cmd) &&
- (lub_string_nocasecmp(iter_cmd, real_prefix)))
- in_iter = iter_cmd + strlen(real_prefix) + 1;
- cmd = clish_view_find_next_completion(view,
- in_iter, in_line, field, this->inherit);
- if (!cmd) {
- lub_string_free(real_prefix);
- return NULL;
- }
- }
- /* If prefix was already returned. */
- if (!cmd && iter_cmd && !lub_string_nocasecmp(iter_cmd, real_prefix)) {
- lub_string_free(real_prefix);
- return NULL;
- }
- retval = clish_nspace_find_create_command(this, real_prefix, cmd);
- lub_string_free(real_prefix);
- if (retval && iter_cmd &&
- lub_string_nocasecmp(iter_cmd, clish_command__get_name(retval)) > 0)
- return NULL;
- return retval;
- }
- /*--------------------------------------------------------- */
- void clish_nspace_clean_proxy(clish_nspace_t * this)
- {
- lub_list_node_t *iter = NULL;
- /* Recursive proxy clean */
- clish_view_clean_proxy(this->view);
- /* Delete each command proxy held by this nspace */
- while ((iter = lub_list__get_tail(this->cmds))) {
- clish_command_delete(lub_list_node__get_data(iter));
- lub_list_del(this->cmds, iter);
- lub_list_node_free(iter);
- }
- }
- CLISH_SET(nspace, bool_t, help);
- CLISH_GET(nspace, bool_t, help);
- CLISH_SET(nspace, bool_t, completion);
- CLISH_GET(nspace, bool_t, completion);
- CLISH_SET(nspace, bool_t, context_help);
- CLISH_GET(nspace, bool_t, context_help);
- CLISH_SET(nspace, bool_t, inherit);
- CLISH_GET(nspace, bool_t, inherit);
- CLISH_SET(nspace, clish_view_t *, view);
- CLISH_GET(nspace, clish_view_t *, view);
- CLISH_SET_STR(nspace, view_name);
- CLISH_GET_STR(nspace, view_name);
- CLISH_SET_STR(nspace, access);
- CLISH_GET_STR(nspace, access);
- CLISH_GET_STR(nspace, prefix);
- /*--------------------------------------------------------- */
- _CLISH_SET_STR_ONCE(nspace, prefix)
- {
- int res = 0;
- assert(inst);
- assert(!inst->prefix);
- res = regcomp(&inst->prefix_regex, val, REG_EXTENDED | REG_ICASE);
- assert(!res);
- inst->prefix = lub_string_dup(val);
- }
- /*--------------------------------------------------------- */
- _CLISH_GET(nspace, const regex_t *, prefix_regex)
- {
- assert(inst);
- if (!inst->prefix)
- return NULL;
- return &inst->prefix_regex;
- }
- /*--------------------------------------------------------- */
- bool_t clish_nspace__get_visibility(const clish_nspace_t * instance,
- clish_nspace_visibility_e field)
- {
- bool_t result = BOOL_FALSE;
- switch (field) {
- case CLISH_NSPACE_HELP:
- result = clish_nspace__get_help(instance);
- break;
- case CLISH_NSPACE_COMPLETION:
- result = clish_nspace__get_completion(instance);
- break;
- case CLISH_NSPACE_CHELP:
- result = clish_nspace__get_context_help(instance);
- break;
- default:
- break;
- }
- return result;
- }
|