kview.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/knspace.h>
  11. #include <klish/kview.h>
  12. struct kview_s {
  13. char *name;
  14. faux_list_t *commands;
  15. faux_list_t *nspaces;
  16. };
  17. // Simple attributes
  18. // Name
  19. KGET_STR(view, name);
  20. // COMMAND list
  21. KGET(view, faux_list_t *, commands);
  22. KCMP_NESTED(view, command, name);
  23. KCMP_NESTED_BY_KEY(view, command, name);
  24. KADD_NESTED(view, kcommand_t *, commands);
  25. KFIND_NESTED(view, command);
  26. KNESTED_LEN(view, commands);
  27. KNESTED_ITER(view, commands);
  28. KNESTED_EACH(view, kcommand_t *, commands);
  29. // NSPACE list
  30. KGET(view, faux_list_t *, nspaces);
  31. KADD_NESTED(view, knspace_t *, nspaces);
  32. KNESTED_LEN(view, nspaces);
  33. KNESTED_ITER(view, nspaces);
  34. KNESTED_EACH(view, knspace_t *, nspaces);
  35. kview_t *kview_new(const char *name)
  36. {
  37. kview_t *view = NULL;
  38. if (faux_str_is_empty(name))
  39. return NULL;
  40. view = faux_zmalloc(sizeof(*view));
  41. assert(view);
  42. if (!view)
  43. return NULL;
  44. // Initialize
  45. view->name = faux_str_dup(name);
  46. // COMMANDs
  47. view->commands = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  48. kview_command_compare, kview_command_kcompare,
  49. (void (*)(void *))kcommand_free);
  50. assert(view->commands);
  51. // NSPACEs
  52. // The order of NSPACEs is the same as order of NSPACE tags.
  53. // The NSPACE need not to be unique because each VIEW can be
  54. // NSPACEd with prefix or without prefix.
  55. view->nspaces = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  56. NULL, NULL, (void (*)(void *))knspace_free);
  57. assert(view->nspaces);
  58. return view;
  59. }
  60. void kview_free(kview_t *view)
  61. {
  62. if (!view)
  63. return;
  64. faux_str_free(view->name);
  65. faux_list_free(view->commands);
  66. faux_list_free(view->nspaces);
  67. faux_free(view);
  68. }