ksession.c 1.7 KB

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