command.c 7.9 KB

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