private.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * shell.h - private interface to the shell class
  3. */
  4. #include "clish/shell.h"
  5. #include "clish/pargv.h"
  6. #include "clish/variable.h"
  7. #include "lub/bintree.h"
  8. #include "tinyrl/tinyrl.h"
  9. /*-------------------------------------
  10. * PRIVATE TYPES
  11. *------------------------------------- */
  12. /*-------------------------------------------------------- */
  13. /*
  14. * The context structure is used to simplify the cleanup of
  15. * a CLI session when a thread is cancelled.
  16. */
  17. struct clish_context_s {
  18. pthread_t pthread;
  19. const clish_shell_hooks_t *hooks;
  20. void *cookie;
  21. FILE *istream;
  22. FILE *ostream;
  23. clish_shell_t *shell;
  24. clish_pargv_t *pargv;
  25. char *prompt;
  26. };
  27. typedef enum {
  28. SHELL_STATE_INITIALISING,
  29. SHELL_STATE_READY,
  30. SHELL_STATE_HELPING,
  31. SHELL_STATE_SCRIPT_ERROR,
  32. SHELL_STATE_CLOSING
  33. } shell_state_t;
  34. /*
  35. * iterate around commands
  36. */
  37. typedef struct {
  38. const char *last_cmd;
  39. clish_nspace_visibility_t field;
  40. } clish_shell_iterator_t;
  41. /* this is used to maintain a stack of file handles */
  42. typedef struct clish_shell_file_s clish_shell_file_t;
  43. struct clish_shell_file_s {
  44. clish_shell_file_t *next;
  45. FILE *file;
  46. bool_t stop_on_error; /* stop on error for file input */
  47. };
  48. typedef struct {
  49. char *line;
  50. clish_view_t *view;
  51. char *viewid;
  52. } clish_shell_pwd_t;
  53. struct clish_shell_s {
  54. lub_bintree_t view_tree; /* Maintain a tree of views */
  55. lub_bintree_t ptype_tree; /* Maintain a tree of ptypes */
  56. const clish_shell_hooks_t *client_hooks; /* Client callback hooks */
  57. void *client_cookie; /* Client callback cookie */
  58. clish_view_t *global; /* Reference to the global view. */
  59. clish_view_t *view; /* Reference to the current view. */
  60. clish_command_t *startup; /* This is the startup command */
  61. clish_shell_iterator_t iter; /* used for iterating commands */
  62. shell_state_t state; /* The current state */
  63. char *overview; /* Overview text for this shell. */
  64. char *viewid; /* The current view ID string */
  65. tinyrl_t *tinyrl; /* Tiny readline instance */
  66. clish_shell_file_t *current_file; /* file currently in use for input */
  67. clish_shell_pwd_t **cfg_pwdv; /* Levels for the config file structure */
  68. unsigned cfg_pwdc;
  69. konf_client_t *client;
  70. clish_pargv_t *completion_pargv;
  71. unsigned completion_index;
  72. unsigned completion_pindex;
  73. clish_param_t *param_depth;
  74. clish_param_t *param_pwd;
  75. };
  76. /**
  77. * Initialise a command iterator structure
  78. */
  79. void
  80. clish_shell_iterator_init(clish_shell_iterator_t * iter,
  81. clish_nspace_visibility_t field);
  82. /**
  83. * get the next command which is an extension of the specified line
  84. */
  85. const clish_command_t *clish_shell_find_next_completion(const clish_shell_t *
  86. instance,
  87. const char *line,
  88. clish_shell_iterator_t *
  89. iter);
  90. /**
  91. * Push the specified file handle on to the stack of file handles
  92. * for this shell. The specified file will become the source of
  93. * commands, until it is exhausted.
  94. *
  95. * \return
  96. * BOOL_TRUE - the file was successfully associated with the shell.
  97. * BOOL_FALSE - there was insufficient resource to associate this file.
  98. */
  99. bool_t
  100. clish_shell_push_file(clish_shell_t * instance,
  101. FILE * file, bool_t stop_on_error);
  102. /**
  103. * Pop the current file handle from the stack of file handles, shutting
  104. * the file down and freeing any associated memory. The next file handle
  105. * in the stack becomes associated with the input stream for this shell.
  106. *
  107. * \return
  108. * BOOL_TRUE - the current file handle has been replaced.
  109. * BOOL_FALSE - there is only one handle on the stack which cannot be replaced.
  110. */
  111. bool_t clish_shell_pop_file(clish_shell_t * instance);
  112. clish_view_t *clish_shell_find_view(clish_shell_t * instance, const char *name);
  113. void clish_shell_insert_view(clish_shell_t * instance, clish_view_t * view);
  114. clish_pargv_status_t
  115. clish_shell_parse(const clish_shell_t * instance,
  116. const char *line,
  117. const clish_command_t ** cmd, clish_pargv_t ** pargv);
  118. char *clish_shell_word_generator(clish_shell_t * instance,
  119. const char *line,
  120. unsigned offset, unsigned state);
  121. const clish_command_t *clish_shell_resolve_command(const clish_shell_t *
  122. instance, const char *line);
  123. const clish_command_t *clish_shell_resolve_prefix(const clish_shell_t *
  124. instance, const char *line);
  125. const clish_command_t *clish_shell_getfirst_command(clish_shell_t * instance,
  126. const char *line,
  127. clish_nspace_visibility_t
  128. field);
  129. const clish_command_t *clish_shell_getnext_command(clish_shell_t * instance,
  130. const char *line);
  131. void clish_shell_insert_ptype(clish_shell_t * instance, clish_ptype_t * ptype);
  132. void clish_shell_tinyrl_history(clish_shell_t * instance, unsigned int *limit);
  133. tinyrl_t *clish_shell_tinyrl_new(FILE * instream,
  134. FILE * outstream, unsigned stifle);
  135. void clish_shell_tinyrl_delete(tinyrl_t * instance);