kview.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. KNESTED_LEN(view, command);
  24. KNESTED_ITER(view, command);
  25. KNESTED_EACH(view, command);
  26. kview_t *kview_new(const char *name)
  27. {
  28. kview_t *view = NULL;
  29. if (faux_str_is_empty(name))
  30. return NULL;
  31. view = faux_zmalloc(sizeof(*view));
  32. assert(view);
  33. if (!view)
  34. return NULL;
  35. // Initialize
  36. view->name = faux_str_dup(name);
  37. view->commands = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  38. kview_command_compare, kview_command_kcompare,
  39. (void (*)(void *))kcommand_free);
  40. assert(view->commands);
  41. return view;
  42. }
  43. void kview_free(kview_t *view)
  44. {
  45. if (!view)
  46. return;
  47. faux_str_free(view->name);
  48. faux_list_free(view->commands);
  49. faux_free(view);
  50. }