kcommand.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/list.h>
  7. #include <klish/kparam.h>
  8. #include <klish/kcommand.h>
  9. struct kcommand_s {
  10. bool_t is_static;
  11. // kaction_error_e error;
  12. icommand_t info;
  13. faux_list_t *params;
  14. };
  15. static int kcommand_param_compare(const void *first, const void *second)
  16. {
  17. const kparam_t *f = (const kparam_t *)first;
  18. const kparam_t *s = (const kparam_t *)second;
  19. return strcmp(kparam_name(f), kparam_name(s));
  20. }
  21. static int kcommand_param_kcompare(const void *key, const void *list_item)
  22. {
  23. const char *f = (const char *)key;
  24. const kparam_t *s = (const kparam_t *)list_item;
  25. return strcmp(f, kparam_name(s));
  26. }
  27. static kcommand_t *kcommand_new_internal(icommand_t info, bool_t is_static)
  28. {
  29. kcommand_t *command = NULL;
  30. command = faux_zmalloc(sizeof(*command));
  31. assert(command);
  32. if (!command)
  33. return NULL;
  34. // Initialize
  35. command->is_static = is_static;
  36. // command->error = KACTION_ERROR_OK;
  37. command->info = info;
  38. // List of parameters
  39. command->params = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  40. kcommand_param_compare, kcommand_param_kcompare,
  41. (void (*)(void *))kparam_free);
  42. assert(command->params);
  43. // if (!command->params) {
  44. // command->error = KACTION_ERROR_LIST;
  45. // return NULL;
  46. // }
  47. // Field "exec_on"
  48. // if (faux_str_casecmp(command->info.
  49. return command;
  50. }
  51. kcommand_t *kcommand_new(icommand_t info)
  52. {
  53. return kcommand_new_internal(info, BOOL_FALSE);
  54. }
  55. kcommand_t *kcommand_new_static(icommand_t info)
  56. {
  57. return kcommand_new_internal(info, BOOL_TRUE);
  58. }
  59. void kcommand_free(kcommand_t *command)
  60. {
  61. if (!command)
  62. return;
  63. if (!command->is_static) {
  64. faux_str_free(command->info.name);
  65. faux_str_free(command->info.help);
  66. }
  67. faux_list_free(command->params);
  68. faux_free(command);
  69. }
  70. const char *kcommand_name(const kcommand_t *command)
  71. {
  72. assert(command);
  73. if (!command)
  74. return NULL;
  75. return command->info.name;
  76. }
  77. const char *kcommand_help(const kcommand_t *command)
  78. {
  79. assert(command);
  80. if (!command)
  81. return NULL;
  82. return command->info.help;
  83. }
  84. bool_t kcommand_add_param(kcommand_t *command, kparam_t *param)
  85. {
  86. assert(command);
  87. if (!command)
  88. return BOOL_FALSE;
  89. assert(param);
  90. if (!param)
  91. return BOOL_FALSE;
  92. if (!faux_list_add(command->params, param))
  93. return BOOL_FALSE;
  94. return BOOL_TRUE;
  95. }