ksession.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/kscheme.h>
  9. #include <klish/kpath.h>
  10. #include <klish/ksession.h>
  11. struct ksession_s {
  12. kscheme_t *scheme;
  13. kpath_t *path;
  14. bool_t done; // Indicates that session is over and must be closed
  15. };
  16. // Scheme
  17. KGET(session, kscheme_t *, scheme);
  18. // Path
  19. KGET(session, kpath_t *, path);
  20. // Done
  21. KGET_BOOL(session, done);
  22. KSET_BOOL(session, done);
  23. ksession_t *ksession_new(kscheme_t *scheme, const char *start_entry)
  24. {
  25. ksession_t *session = NULL;
  26. const kentry_t *entry = NULL;
  27. const char *entry_to_search = NULL;
  28. klevel_t *level = NULL;
  29. assert(scheme);
  30. if (!scheme)
  31. return NULL;
  32. // Before real session allocation we will try to find starting entry.
  33. // Starting entry can be get from function argument, from STARTUP tag or
  34. // default name 'main' can be used. Don't create session if we can't get
  35. // starting entry at all. Priorities are (from higher) argument, STARTUP,
  36. // default name.
  37. if (start_entry)
  38. entry_to_search = start_entry;
  39. // STARTUP is not implemented yet
  40. else
  41. entry_to_search = KSESSION_STARTING_ENTRY;
  42. entry = kscheme_find_entry_by_path(scheme, entry_to_search);
  43. if (!entry)
  44. return NULL; // Can't find starting entry
  45. session = faux_zmalloc(sizeof(*session));
  46. assert(session);
  47. if (!session)
  48. return NULL;
  49. // Initialization
  50. session->scheme = scheme;
  51. // Create kpath_t stack
  52. session->path = kpath_new();
  53. assert(session->path);
  54. level = klevel_new(entry);
  55. assert(level);
  56. kpath_push(session->path, level);
  57. session->done = BOOL_FALSE;
  58. return session;
  59. }
  60. void ksession_free(ksession_t *session)
  61. {
  62. if (!session)
  63. return;
  64. kpath_free(session->path);
  65. free(session);
  66. }