command.c 13 KB

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