command.c 14 KB

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