command.c 16 KB

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