1
0

command.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * command.c
  3. *
  4. * This file provides the implementation of a command definition
  5. */
  6. #include "private.h"
  7. #include "clish/variable.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. /*---------------------------------------------------------
  15. * PRIVATE METHODS
  16. *--------------------------------------------------------- */
  17. static void
  18. clish_command_init(clish_command_t * this, const char *name, const char *text)
  19. {
  20. /* initialise the node part */
  21. this->name = lub_string_dup(name);
  22. this->text = lub_string_dup(text);
  23. /* Be a good binary tree citizen */
  24. lub_bintree_node_init(&this->bt_node);
  25. /* set up defaults */
  26. this->link = BOOL_FALSE;
  27. this->paramv = clish_paramv_new();
  28. this->viewid = NULL;
  29. this->view = NULL;
  30. this->action = NULL;
  31. this->detail = NULL;
  32. this->builtin = NULL;
  33. this->escape_chars = NULL;
  34. this->args = NULL;
  35. this->pview = NULL;
  36. this->cfg_op = CLISH_CONFIG_NONE;
  37. this->priority = 0x7f00; /* medium priority by default */
  38. this->pattern = NULL;
  39. this->file = NULL;
  40. this->splitter = BOOL_TRUE;
  41. }
  42. /*--------------------------------------------------------- */
  43. static void clish_command_fini(clish_command_t * this)
  44. {
  45. unsigned i;
  46. lub_string_free(this->name);
  47. this->name = NULL;
  48. /* Link need not full cleanup */
  49. if (this->link)
  50. return;
  51. /* finalize each of the parameter instances */
  52. clish_paramv_delete(this->paramv);
  53. lub_string_free(this->viewid);
  54. this->viewid = NULL;
  55. lub_string_free(this->action);
  56. this->action = NULL;
  57. lub_string_free(this->text);
  58. this->text = NULL;
  59. lub_string_free(this->detail);
  60. this->detail = NULL;
  61. lub_string_free(this->builtin);
  62. this->builtin = NULL;
  63. lub_string_free(this->escape_chars);
  64. this->escape_chars = NULL;
  65. if (NULL != this->args) {
  66. clish_param_delete(this->args);
  67. this->args = NULL;
  68. }
  69. this->pview = NULL;
  70. lub_string_free(this->pattern);
  71. this->pattern = NULL;
  72. lub_string_free(this->file);
  73. this->file = NULL;
  74. }
  75. /*---------------------------------------------------------
  76. * PUBLIC META FUNCTIONS
  77. *--------------------------------------------------------- */
  78. size_t clish_command_bt_offset(void)
  79. {
  80. return offsetof(clish_command_t, bt_node);
  81. }
  82. /*--------------------------------------------------------- */
  83. int clish_command_bt_compare(const void *clientnode, const void *clientkey)
  84. {
  85. const clish_command_t *this = clientnode;
  86. const char *key = clientkey;
  87. return lub_string_nocasecmp(this->name, key);
  88. }
  89. /*--------------------------------------------------------- */
  90. void clish_command_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  91. {
  92. const clish_command_t *this = clientnode;
  93. /* fill out the opaque key */
  94. strcpy((char *)key, this->name);
  95. }
  96. /*--------------------------------------------------------- */
  97. clish_command_t *clish_command_new(const char *name, const char *text)
  98. {
  99. clish_command_t *this = malloc(sizeof(clish_command_t));
  100. if (this) {
  101. clish_command_init(this, name, text);
  102. }
  103. return this;
  104. }
  105. /*--------------------------------------------------------- */
  106. clish_command_t *clish_command_new_link(const char *name,
  107. const clish_command_t * ref)
  108. {
  109. if (!ref)
  110. return NULL;
  111. clish_command_t *this = malloc(sizeof(clish_command_t));
  112. assert(this);
  113. /* Copy all fields to the new command-link */
  114. *this = *ref;
  115. /* Initialise the name (other than original name) */
  116. this->name = lub_string_dup(name);
  117. /* Be a good binary tree citizen */
  118. lub_bintree_node_init(&this->bt_node);
  119. /* It a link to command so set the link flag */
  120. this->link = BOOL_TRUE;
  121. return this;
  122. }
  123. /*---------------------------------------------------------
  124. * PUBLIC METHODS
  125. *--------------------------------------------------------- */
  126. void clish_command_delete(clish_command_t * this)
  127. {
  128. clish_command_fini(this);
  129. free(this);
  130. }
  131. /*--------------------------------------------------------- */
  132. void clish_command_insert_param(clish_command_t * this, clish_param_t * param)
  133. {
  134. clish_paramv_insert(this->paramv, param);
  135. }
  136. /*--------------------------------------------------------- */
  137. void clish_command_help(const clish_command_t * this, const char *line)
  138. {
  139. const char *name = clish_command__get_name(this);
  140. unsigned index = lub_argv_wordcount(line);
  141. const clish_param_t *param;
  142. unsigned idx = lub_argv_wordcount(name);
  143. lub_argv_t *argv;
  144. clish_pargv_t *last, *pargv;
  145. unsigned i;
  146. unsigned cnt = 0;
  147. unsigned longest = 0;
  148. if (0 == index)
  149. return;
  150. /* Line has no any parameters */
  151. if (strlen(line) <= strlen(name)) {
  152. printf("%s %s\n", name,
  153. clish_command__get_text(this));
  154. return;
  155. }
  156. if (line[strlen(line) - 1] != ' ')
  157. index--;
  158. argv = lub_argv_new(line, 0);
  159. /* get the parameter definition */
  160. last = clish_pargv_create();
  161. pargv = clish_pargv_create();
  162. clish_pargv_parse(pargv, this, this->paramv,
  163. argv, &idx, last, index);
  164. clish_pargv_delete(pargv);
  165. cnt = clish_pargv__get_count(last);
  166. /* Calculate the longest name */
  167. for (i = 0; i < cnt; i++) {
  168. const clish_param_t *param;
  169. const char *name;
  170. unsigned clen = 0;
  171. param = clish_pargv__get_param(last, i);
  172. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(param))
  173. name = clish_param__get_name(param);
  174. else
  175. name = clish_ptype__get_text(clish_param__get_ptype(param));
  176. if (name)
  177. clen = strlen(name);
  178. if (clen > longest)
  179. longest = clen;
  180. }
  181. for (i = 0; i < cnt; i++)
  182. clish_param_help(clish_pargv__get_param(last, i), longest + 1);
  183. clish_pargv_delete(last);
  184. lub_argv_delete(argv);
  185. }
  186. /*--------------------------------------------------------- */
  187. clish_command_t *clish_command_choose_longest(clish_command_t * cmd1,
  188. clish_command_t * cmd2)
  189. {
  190. unsigned len1 = (cmd1 ? strlen(clish_command__get_name(cmd1)) : 0);
  191. unsigned len2 = (cmd2 ? strlen(clish_command__get_name(cmd2)) : 0);
  192. if (len2 < len1) {
  193. return cmd1;
  194. } else if (len1 < len2) {
  195. return cmd2;
  196. } else {
  197. /* let local view override */
  198. return cmd1;
  199. }
  200. }
  201. /*--------------------------------------------------------- */
  202. int
  203. clish_command_diff(const clish_command_t * cmd1, const clish_command_t * cmd2)
  204. {
  205. if (NULL == cmd1) {
  206. if (NULL != cmd2) {
  207. return 1;
  208. } else {
  209. return 0;
  210. }
  211. }
  212. if (NULL == cmd2) {
  213. return -1;
  214. }
  215. return lub_string_nocasecmp(clish_command__get_name(cmd1),
  216. clish_command__get_name(cmd2));
  217. }
  218. /*---------------------------------------------------------
  219. * PUBLIC ATTRIBUTES
  220. *--------------------------------------------------------- */
  221. const char *clish_command__get_name(const clish_command_t * this)
  222. {
  223. return this->name;
  224. }
  225. /*--------------------------------------------------------- */
  226. const char *clish_command__get_text(const clish_command_t * this)
  227. {
  228. return this->text;
  229. }
  230. /*--------------------------------------------------------- */
  231. void clish_command__set_action(clish_command_t * this, const char *action)
  232. {
  233. assert(NULL == this->action);
  234. this->action = lub_string_dup(action);
  235. }
  236. /*--------------------------------------------------------- */
  237. const char *clish_command__get_detail(const clish_command_t * this)
  238. {
  239. return this->detail;
  240. }
  241. /*--------------------------------------------------------- */
  242. void clish_command__set_detail(clish_command_t * this, const char *detail)
  243. {
  244. assert(NULL == this->detail);
  245. this->detail = lub_string_dup(detail);
  246. }
  247. /*--------------------------------------------------------- */
  248. char *clish_command__get_action(const clish_command_t * this,
  249. const char *viewid, clish_pargv_t * pargv)
  250. {
  251. return clish_variable_expand(this->action, viewid, this, pargv);
  252. }
  253. /*--------------------------------------------------------- */
  254. void clish_command__set_view(clish_command_t * this, clish_view_t * view)
  255. {
  256. assert(NULL == this->view);
  257. this->view = view;
  258. }
  259. /*--------------------------------------------------------- */
  260. clish_view_t *clish_command__get_view(const clish_command_t * this)
  261. {
  262. return this->view;
  263. }
  264. /*--------------------------------------------------------- */
  265. void clish_command__set_viewid(clish_command_t * this, const char *viewid)
  266. {
  267. assert(NULL == this->viewid);
  268. this->viewid = lub_string_dup(viewid);
  269. }
  270. /*--------------------------------------------------------- */
  271. char *clish_command__get_viewid(const clish_command_t * this,
  272. const char *viewid, clish_pargv_t * pargv)
  273. {
  274. return clish_variable_expand(this->viewid, viewid, this, pargv);
  275. }
  276. /*--------------------------------------------------------- */
  277. const clish_param_t *clish_command__get_param(const clish_command_t * this,
  278. unsigned index)
  279. {
  280. return clish_paramv__get_param(this->paramv, index);
  281. }
  282. /*--------------------------------------------------------- */
  283. const char *clish_command__get_suffix(const clish_command_t * this)
  284. {
  285. return lub_string_suffix(this->name);
  286. }
  287. /*--------------------------------------------------------- */
  288. void clish_command__set_builtin(clish_command_t * this, const char *builtin)
  289. {
  290. assert(NULL == this->builtin);
  291. this->builtin = lub_string_dup(builtin);
  292. }
  293. /*--------------------------------------------------------- */
  294. const char *clish_command__get_builtin(const clish_command_t * this)
  295. {
  296. return this->builtin;
  297. }
  298. /*--------------------------------------------------------- */
  299. void
  300. clish_command__set_escape_chars(clish_command_t * this,
  301. const char *escape_chars)
  302. {
  303. assert(NULL == this->escape_chars);
  304. this->escape_chars = lub_string_dup(escape_chars);
  305. }
  306. /*--------------------------------------------------------- */
  307. const char *clish_command__get_escape_chars(const clish_command_t * this)
  308. {
  309. return this->escape_chars;
  310. }
  311. /*--------------------------------------------------------- */
  312. void clish_command__set_args(clish_command_t * this, clish_param_t * args)
  313. {
  314. assert(NULL == this->args);
  315. this->args = args;
  316. }
  317. /*--------------------------------------------------------- */
  318. const clish_param_t *clish_command__get_args(const clish_command_t * this)
  319. {
  320. return this->args;
  321. }
  322. /*--------------------------------------------------------- */
  323. const unsigned clish_command__get_param_count(const clish_command_t * this)
  324. {
  325. return clish_paramv__get_count(this->paramv);
  326. }
  327. /*--------------------------------------------------------- */
  328. clish_paramv_t *clish_command__get_paramv(const clish_command_t * this)
  329. {
  330. return this->paramv;
  331. }
  332. /*--------------------------------------------------------- */
  333. void clish_command__set_pview(clish_command_t * this, clish_view_t * view)
  334. {
  335. this->pview = view;
  336. }
  337. /*--------------------------------------------------------- */
  338. clish_view_t *clish_command__get_pview(const clish_command_t * this)
  339. {
  340. return this->pview;
  341. }
  342. /*--------------------------------------------------------- */
  343. unsigned clish_command__get_depth(const clish_command_t * this)
  344. {
  345. if (!this->pview)
  346. return 0;
  347. return clish_view__get_depth(this->pview);
  348. }
  349. /*--------------------------------------------------------- */
  350. void
  351. clish_command__set_cfg_op(clish_command_t * this,
  352. clish_config_operation_t operation)
  353. {
  354. this->cfg_op = operation;
  355. }
  356. /*--------------------------------------------------------- */
  357. clish_config_operation_t clish_command__get_cfg_op(const clish_command_t * this)
  358. {
  359. return this->cfg_op;
  360. }
  361. /*--------------------------------------------------------- */
  362. void clish_command__set_priority(clish_command_t * this, unsigned short priority)
  363. {
  364. this->priority = priority;
  365. }
  366. /*--------------------------------------------------------- */
  367. unsigned short clish_command__get_priority(const clish_command_t * this)
  368. {
  369. return this->priority;
  370. }
  371. /*--------------------------------------------------------- */
  372. void clish_command__set_pattern(clish_command_t * this, const char *pattern)
  373. {
  374. assert(NULL == this->pattern);
  375. this->pattern = lub_string_dup(pattern);
  376. }
  377. /*--------------------------------------------------------- */
  378. char *clish_command__get_pattern(const clish_command_t * this,
  379. clish_pargv_t * pargv)
  380. {
  381. return clish_variable_expand(this->pattern, this->viewid, this, pargv);
  382. }
  383. /*--------------------------------------------------------- */
  384. void clish_command__set_file(clish_command_t * this, const char *file)
  385. {
  386. assert(NULL == this->file);
  387. this->file = lub_string_dup(file);
  388. }
  389. /*--------------------------------------------------------- */
  390. char *clish_command__get_file(const clish_command_t * this,
  391. clish_pargv_t * pargv)
  392. {
  393. return clish_variable_expand(this->file, this->viewid, this, pargv);
  394. }
  395. /*--------------------------------------------------------- */
  396. bool_t clish_command__get_splitter(const clish_command_t * this)
  397. {
  398. return this->splitter;
  399. }
  400. /*--------------------------------------------------------- */
  401. void clish_command__set_splitter(clish_command_t * this, bool_t splitter)
  402. {
  403. this->splitter = splitter;
  404. }