ksession.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /** @file ksession.c
  2. */
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <klish/khelper.h>
  8. #include <klish/kview.h>
  9. #include <klish/kscheme.h>
  10. #include <klish/kpath.h>
  11. #include <klish/ksession.h>
  12. struct ksession_s {
  13. const kscheme_t *scheme;
  14. kpath_t *path;
  15. };
  16. // Scheme
  17. KGET(session, const kscheme_t *, scheme);
  18. // Path
  19. KGET(session, kpath_t *, path);
  20. ksession_t *ksession_new(const kscheme_t *scheme, const char *start_view)
  21. {
  22. ksession_t *session = NULL;
  23. kview_t *view = NULL;
  24. const char *view_to_search = NULL;
  25. klevel_t *level = NULL;
  26. assert(scheme);
  27. if (!scheme)
  28. return NULL;
  29. // Before real session allocation we will try to find starting view.
  30. // Starting view can be get from function argument, from STARTUP tag or
  31. // default name 'main' can be used. Don't create session if we can't get
  32. // starting view at all. Priorities are (from higher) argument, STARTUP,
  33. // default name.
  34. if (start_view)
  35. view_to_search = start_view;
  36. // STARTUP is not implemented yet
  37. else
  38. view_to_search = KSESSION_DEFAULT_VIEW;
  39. view = kscheme_find_view(scheme, view_to_search);
  40. if (view)
  41. return NULL; // Can't find starting view
  42. session = faux_zmalloc(sizeof(*session));
  43. assert(session);
  44. if (!session)
  45. return NULL;
  46. // Initialization
  47. session->scheme = scheme;
  48. // Create kpath_t stack
  49. session->path = kpath_new();
  50. assert(session->path);
  51. level = klevel_new(view);
  52. assert(level);
  53. kpath_push(session->path, level);
  54. return session;
  55. }
  56. void ksession_free(ksession_t *session)
  57. {
  58. if (!session)
  59. return;
  60. kpath_free(session->path);
  61. free(session);
  62. }
  63. //bool_t ksession_parse_command(const kscheme_t scheme,