command.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * command.c
  3. *
  4. * This file provides the implementation of a command definition
  5. */
  6. #include "private.h"
  7. #include "clish/types.h"
  8. #include "lub/bintree.h"
  9. #include "lub/string.h"
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. static void clish_command_init(clish_command_t *this,
  15. const char *name, const char *text)
  16. {
  17. this->name = lub_string_dup(name);
  18. this->text = lub_string_dup(text);
  19. /* Defaults */
  20. this->link = NULL;
  21. this->alias = NULL;
  22. this->alias_view = NULL;
  23. this->paramv = clish_paramv_new();
  24. this->viewid = NULL;
  25. this->viewname = NULL;
  26. this->action = clish_action_new();
  27. this->config = clish_config_new();
  28. this->detail = NULL;
  29. this->escape_chars = NULL;
  30. this->regex_chars = NULL;
  31. this->args = NULL;
  32. this->pview = NULL;
  33. this->dynamic = BOOL_FALSE;
  34. this->internal = BOOL_FALSE;
  35. this->access = NULL;
  36. }
  37. /*--------------------------------------------------------- */
  38. static void clish_command_fini(clish_command_t * this)
  39. {
  40. lub_string_free(this->name);
  41. lub_string_free(this->text);
  42. /* Link need not full cleanup */
  43. if (this->link)
  44. return;
  45. /* finalize each of the parameter instances */
  46. clish_paramv_delete(this->paramv);
  47. clish_action_delete(this->action);
  48. clish_config_delete(this->config);
  49. lub_string_free(this->alias);
  50. lub_string_free(this->alias_view);
  51. lub_string_free(this->viewname);
  52. lub_string_free(this->viewid);
  53. lub_string_free(this->detail);
  54. lub_string_free(this->escape_chars);
  55. lub_string_free(this->regex_chars);
  56. lub_string_free(this->access);
  57. if (this->args)
  58. clish_param_delete(this->args);
  59. }
  60. /*--------------------------------------------------------- */
  61. int clish_command_compare(const void *first, const void *second)
  62. {
  63. const clish_command_t *f = (const clish_command_t *)first;
  64. const clish_command_t *s = (const clish_command_t *)second;
  65. return lub_string_nocasecmp(f->name, s->name);
  66. }
  67. /*--------------------------------------------------------- */
  68. clish_command_t *clish_command_new(const char *name, const char *help)
  69. {
  70. clish_command_t *this = malloc(sizeof(clish_command_t));
  71. if (this)
  72. clish_command_init(this, name, help);
  73. return this;
  74. }
  75. /*--------------------------------------------------------- */
  76. clish_command_t *clish_command_new_link(const char *name,
  77. const char *help, const clish_command_t * ref)
  78. {
  79. if (!ref)
  80. return NULL;
  81. clish_command_t *this = malloc(sizeof(clish_command_t));
  82. assert(this);
  83. /* Copy all fields to the new command-link */
  84. *this = *ref;
  85. /* Initialise the name (other than original name) */
  86. this->name = lub_string_dup(name);
  87. /* Initialise the help (other than original help) */
  88. this->text = lub_string_dup(help);
  89. /* It a link to command so set the link flag */
  90. this->link = ref;
  91. return this;
  92. }
  93. /*--------------------------------------------------------- */
  94. clish_command_t * clish_command_alias_to_link(clish_command_t *this, clish_command_t *ref)
  95. {
  96. clish_command_t tmp;
  97. if (!this || !ref)
  98. return NULL;
  99. if (ref->alias) /* The reference is a link too */
  100. return NULL;
  101. memcpy(&tmp, this, sizeof(tmp));
  102. *this = *ref;
  103. this->name = lub_string_dup(tmp.name); /* Save an original name */
  104. this->text = lub_string_dup(tmp.text); /* Save an original help */
  105. this->link = ref;
  106. this->pview = tmp.pview; /* Save an original parent view */
  107. clish_command_fini(&tmp);
  108. return this;
  109. }
  110. /*--------------------------------------------------------- */
  111. void clish_command_delete(void *data)
  112. {
  113. clish_command_t *this = (clish_command_t *)data;
  114. clish_command_fini(this);
  115. free(this);
  116. }
  117. /*--------------------------------------------------------- */
  118. void clish_command_insert_param(clish_command_t * this, clish_param_t * param)
  119. {
  120. clish_paramv_insert(this->paramv, param);
  121. }
  122. /*--------------------------------------------------------- */
  123. int clish_command_help(const clish_command_t *this)
  124. {
  125. this = this; /* Happy compiler */
  126. return 0;
  127. }
  128. /*--------------------------------------------------------- */
  129. clish_command_t *clish_command_choose_longest(clish_command_t * cmd1,
  130. clish_command_t * cmd2)
  131. {
  132. unsigned len1 = (cmd1 ? strlen(clish_command__get_name(cmd1)) : 0);
  133. unsigned len2 = (cmd2 ? strlen(clish_command__get_name(cmd2)) : 0);
  134. if (len2 < len1) {
  135. return cmd1;
  136. } else if (len1 < len2) {
  137. return cmd2;
  138. } else {
  139. /* let local view override */
  140. return cmd1;
  141. }
  142. }
  143. /*--------------------------------------------------------- */
  144. int clish_command_diff(const clish_command_t * cmd1,
  145. const clish_command_t * cmd2)
  146. {
  147. if (NULL == cmd1) {
  148. if (NULL != cmd2)
  149. return 1;
  150. else
  151. return 0;
  152. }
  153. if (NULL == cmd2)
  154. return -1;
  155. return lub_string_nocasecmp(clish_command__get_name(cmd1),
  156. clish_command__get_name(cmd2));
  157. }
  158. CLISH_GET_STR(command, name);
  159. CLISH_GET_STR(command, text);
  160. CLISH_SET_STR_ONCE(command, detail);
  161. CLISH_GET_STR(command, detail);
  162. CLISH_GET(command, clish_action_t *, action);
  163. CLISH_GET(command, clish_config_t *, config);
  164. CLISH_SET_STR_ONCE(command, regex_chars);
  165. CLISH_GET_STR(command, regex_chars);
  166. CLISH_SET_STR_ONCE(command, escape_chars);
  167. CLISH_GET_STR(command, escape_chars);
  168. CLISH_SET_STR_ONCE(command, viewname);
  169. CLISH_GET_STR(command, viewname);
  170. CLISH_SET_STR_ONCE(command, viewid);
  171. CLISH_GET_STR(command, viewid);
  172. CLISH_SET_ONCE(command, clish_param_t *, args);
  173. CLISH_GET(command, clish_param_t *, args);
  174. CLISH_GET(command, clish_paramv_t *, paramv);
  175. CLISH_SET(command, clish_view_t *, pview);
  176. CLISH_GET(command, clish_view_t *, pview);
  177. CLISH_SET_STR(command, access);
  178. CLISH_GET_STR(command, access);
  179. CLISH_SET_STR(command, alias);
  180. CLISH_GET_STR(command, alias);
  181. CLISH_SET_STR(command, alias_view);
  182. CLISH_GET_STR(command, alias_view);
  183. CLISH_SET(command, bool_t, internal);
  184. CLISH_GET(command, bool_t, internal);
  185. CLISH_SET(command, bool_t, dynamic);
  186. CLISH_GET(command, bool_t, dynamic);
  187. /*--------------------------------------------------------- */
  188. void clish_command__force_viewname(clish_command_t * this, const char *viewname)
  189. {
  190. if (this->viewname)
  191. lub_string_free(this->viewname);
  192. this->viewname = lub_string_dup(viewname);
  193. }
  194. /*--------------------------------------------------------- */
  195. void clish_command__force_viewid(clish_command_t * this, const char *viewid)
  196. {
  197. if (this->viewid)
  198. lub_string_free(this->viewid);
  199. this->viewid = lub_string_dup(viewid);
  200. }
  201. /*--------------------------------------------------------- */
  202. const clish_param_t *clish_command__get_param(const clish_command_t * this,
  203. unsigned index)
  204. {
  205. return clish_paramv__get_param(this->paramv, index);
  206. }
  207. /*--------------------------------------------------------- */
  208. const char *clish_command__get_suffix(const clish_command_t * this)
  209. {
  210. return lub_string_suffix(this->name);
  211. }
  212. /*--------------------------------------------------------- */
  213. unsigned int clish_command__get_param_count(const clish_command_t * this)
  214. {
  215. return clish_paramv__get_count(this->paramv);
  216. }
  217. /*--------------------------------------------------------- */
  218. int clish_command__get_depth(const clish_command_t * this)
  219. {
  220. if (!this->pview)
  221. return 0;
  222. return clish_view__get_depth(this->pview);
  223. }
  224. /*--------------------------------------------------------- */
  225. clish_view_restore_e clish_command__get_restore(const clish_command_t * this)
  226. {
  227. if (!this->pview)
  228. return CLISH_RESTORE_NONE;
  229. return clish_view__get_restore(this->pview);
  230. }
  231. /*--------------------------------------------------------- */
  232. const clish_command_t * clish_command__get_orig(const clish_command_t * this)
  233. {
  234. if (this->link)
  235. return clish_command__get_orig(this->link);
  236. return this;
  237. }
  238. /*--------------------------------------------------------- */
  239. const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
  240. {
  241. if (!this->dynamic)
  242. return this;
  243. if (this->link)
  244. return clish_command__get_cmd(this->link);
  245. return NULL;
  246. }
  247. /*--------------------------------------------------------- */
  248. int clish_command_fn_find_by_name(const void *key, const void *data) {
  249. const char *name = (const char *)key;
  250. const clish_command_t *d = (const clish_command_t *)data;
  251. return lub_string_nocasecmp(name, clish_command__get_name(d));
  252. }