ksession.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. size_t term_width;
  16. size_t term_height;
  17. };
  18. // Scheme
  19. KGET(session, kscheme_t *, scheme);
  20. // Path
  21. KGET(session, kpath_t *, path);
  22. // Done
  23. KGET_BOOL(session, done);
  24. KSET_BOOL(session, done);
  25. // Width of pseudo terminal
  26. KGET(session, size_t, term_width);
  27. KSET(session, size_t, term_width);
  28. // Height of pseudo terminal
  29. KGET(session, size_t, term_height);
  30. KSET(session, size_t, term_height);
  31. ksession_t *ksession_new(kscheme_t *scheme, const char *start_entry)
  32. {
  33. ksession_t *session = NULL;
  34. const kentry_t *entry = NULL;
  35. const char *entry_to_search = NULL;
  36. klevel_t *level = NULL;
  37. assert(scheme);
  38. if (!scheme)
  39. return NULL;
  40. // Before real session allocation we will try to find starting entry.
  41. // Starting entry can be get from function argument, from STARTUP tag or
  42. // default name 'main' can be used. Don't create session if we can't get
  43. // starting entry at all. Priorities are (from higher) argument, STARTUP,
  44. // default name.
  45. if (start_entry)
  46. entry_to_search = start_entry;
  47. // STARTUP is not implemented yet
  48. else
  49. entry_to_search = KSESSION_STARTING_ENTRY;
  50. entry = kscheme_find_entry_by_path(scheme, entry_to_search);
  51. if (!entry)
  52. return NULL; // Can't find starting entry
  53. session = faux_zmalloc(sizeof(*session));
  54. assert(session);
  55. if (!session)
  56. return NULL;
  57. // Initialization
  58. session->scheme = scheme;
  59. // Create kpath_t stack
  60. session->path = kpath_new();
  61. assert(session->path);
  62. level = klevel_new(entry);
  63. assert(level);
  64. kpath_push(session->path, level);
  65. session->done = BOOL_FALSE;
  66. session->term_width = 0;
  67. session->term_height = 0;
  68. return session;
  69. }
  70. void ksession_free(ksession_t *session)
  71. {
  72. if (!session)
  73. return;
  74. kpath_free(session->path);
  75. free(session);
  76. }