command.c 15 KB

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