kview.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 <faux/error.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kcommand.h>
  10. #include <klish/kview.h>
  11. struct kview_s {
  12. char *name;
  13. faux_list_t *commands;
  14. };
  15. // Simple attributes
  16. // Name
  17. KGET_STR(view, name);
  18. // COMMAND list
  19. KCMP_NESTED(view, command, name);
  20. KCMP_NESTED_BY_KEY(view, command, name);
  21. KADD_NESTED(view, command);
  22. KFIND_NESTED(view, command);
  23. kview_t *kview_new(const char *name)
  24. {
  25. kview_t *view = NULL;
  26. if (faux_str_is_empty(name))
  27. return NULL;
  28. view = faux_zmalloc(sizeof(*view));
  29. assert(view);
  30. if (!view)
  31. return NULL;
  32. // Initialize
  33. view->name = faux_str_dup(name);
  34. view->commands = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  35. kview_command_compare, kview_command_kcompare,
  36. (void (*)(void *))kcommand_free);
  37. assert(view->commands);
  38. return view;
  39. }
  40. void kview_free(kview_t *view)
  41. {
  42. if (!view)
  43. return;
  44. faux_str_free(view->name);
  45. faux_list_free(view->commands);
  46. faux_free(view);
  47. }