shell_command_generator.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * shell_word_generator.c
  3. */
  4. #include <string.h>
  5. #include "private.h"
  6. #include "lub/string.h"
  7. #include "lub/argv.h"
  8. /*-------------------------------------------------------- */
  9. void
  10. clish_shell_iterator_init(clish_shell_iterator_t * iter,
  11. clish_nspace_visibility_t field)
  12. {
  13. iter->last_cmd = NULL;
  14. iter->field = field;
  15. }
  16. /*-------------------------------------------------------- */
  17. const clish_command_t *clish_shell_find_next_completion(const clish_shell_t *
  18. this, const char *line,
  19. clish_shell_iterator_t *
  20. iter)
  21. {
  22. const clish_command_t *result, *cmd;
  23. clish_nspace_t *nspace;
  24. clish_view_t *view;
  25. unsigned view_cnt = clish_view__get_nspace_count(this->view);
  26. int i;
  27. /* ask the local view for next command */
  28. result = clish_view_find_next_completion(this->view,
  29. iter->last_cmd, line, iter->field, BOOL_TRUE);
  30. /* ask the global view for next command */
  31. cmd = clish_view_find_next_completion(this->global,
  32. iter->last_cmd, line, iter->field, BOOL_TRUE);
  33. if (clish_command_diff(result, cmd) > 0)
  34. result = cmd;
  35. if (!result)
  36. iter->last_cmd = NULL;
  37. else
  38. iter->last_cmd = clish_command__get_name(result);
  39. return result;
  40. }
  41. /*--------------------------------------------------------- */
  42. static char *clish_shell_param_generator(clish_shell_t * this,
  43. const clish_command_t * cmd,
  44. const char *line,
  45. unsigned offset, unsigned state)
  46. {
  47. char *result = NULL;
  48. const char *name = clish_command__get_name(cmd);
  49. char *text = lub_string_dup(&line[offset]);
  50. unsigned index;
  51. const clish_param_t *param = NULL;
  52. clish_ptype_t *ptype;
  53. unsigned idx;
  54. /* get the index of the current parameter */
  55. index = lub_argv_wordcount(line) - lub_argv_wordcount(name);
  56. if ((0 != index) || (line[offset - 1] == ' ')) {
  57. if (0 == state) {
  58. lub_argv_t *argv;
  59. clish_pargv_t *pargv;
  60. unsigned i;
  61. if ((0 != index) && (text[0] != '\0')) {
  62. /* if there is some text for the parameter then adjust the index */
  63. index--;
  64. }
  65. argv = lub_argv_new(line, 0);
  66. idx = lub_argv_wordcount(name);
  67. if (this->completion_pargv) {
  68. clish_pargv_delete(this->completion_pargv);
  69. this->completion_pargv = NULL;
  70. }
  71. this->completion_pargv = clish_pargv_create();
  72. pargv = clish_pargv_create();
  73. clish_pargv_parse(pargv, cmd, clish_command__get_paramv(cmd),
  74. argv, &idx, this->completion_pargv, index + idx);
  75. clish_pargv_delete(pargv);
  76. lub_argv_delete(argv);
  77. this->completion_index = 0;
  78. this->completion_pindex = 0;
  79. }
  80. while ((param = clish_pargv__get_param(this->completion_pargv,
  81. this->completion_index++))) {
  82. if (param == clish_command__get_args(cmd)) {
  83. /* The param is args so it has no format */
  84. result = lub_string_dup(text);
  85. } else if (CLISH_PARAM_SUBCOMMAND ==
  86. clish_param__get_mode(param)) {
  87. /* The subcommand is identified by it's value */
  88. result = lub_string_dup(clish_param__get_value(param));
  89. } else if (CLISH_PARAM_SWITCH ==
  90. clish_param__get_mode(param)) {
  91. /* The switch has no completion string */
  92. result = NULL;
  93. } else {
  94. /* The common param. Let ptype do the work */
  95. if (ptype = clish_param__get_ptype(param)) {
  96. result = clish_ptype_word_generator(ptype, text,
  97. this->completion_pindex++);
  98. if (!result)
  99. this->completion_pindex = 0;
  100. else
  101. this->completion_index--;
  102. } else {
  103. result = NULL;
  104. }
  105. }
  106. if (result)
  107. break;
  108. }
  109. } else if (0 == state) {
  110. /* simply return the command name */
  111. result = lub_string_dup(clish_command__get_suffix(cmd));
  112. }
  113. if (!result) {
  114. clish_pargv_delete(this->completion_pargv);
  115. this->completion_pargv = NULL;
  116. /* make sure we reset the line state */
  117. // tinyrl_crlf(this->tinyrl);
  118. // tinyrl_reset_line_state(this->tinyrl);
  119. // tinyrl_completion_error_over(this->tinyrl);
  120. }
  121. lub_string_free(text);
  122. return result;
  123. }
  124. /*--------------------------------------------------------- */
  125. static char *clish_shell_command_generator(clish_shell_t * this,
  126. const char *line,
  127. unsigned offset, unsigned state)
  128. {
  129. char *result = NULL;
  130. const clish_command_t *cmd = NULL;
  131. if (0 == state) {
  132. cmd =
  133. clish_shell_getfirst_command(this, line,
  134. CLISH_NSPACE_COMPLETION);
  135. } else {
  136. cmd = clish_shell_getnext_command(this, line);
  137. }
  138. if (NULL != cmd) {
  139. result = lub_string_dup(clish_command__get_suffix(cmd));
  140. }
  141. /* keep the compiler happy */
  142. offset = offset;
  143. return result;
  144. }
  145. /*--------------------------------------------------------- */
  146. char *clish_shell_word_generator(clish_shell_t * this,
  147. const char *line,
  148. unsigned offset, unsigned state)
  149. {
  150. char *result = NULL;
  151. const clish_command_t *cmd, *next = NULL;
  152. /* try and resolve a command which is a prefix of the line */
  153. cmd = clish_shell_resolve_command(this, line);
  154. if (NULL != cmd) {
  155. clish_shell_iterator_t iter;
  156. /* see whether there is an extended extension */
  157. clish_shell_iterator_init(&iter, CLISH_NSPACE_COMPLETION);
  158. next = clish_shell_find_next_completion(this, line, &iter);
  159. }
  160. if ((NULL != cmd) && (NULL == next)) {
  161. /* this needs to be completed as a parameter */
  162. result =
  163. clish_shell_param_generator(this, cmd, line, offset,
  164. state);
  165. } else {
  166. /* this needs to be completed as a command */
  167. result =
  168. clish_shell_command_generator(this, line, offset, state);
  169. }
  170. if (0 == state) {
  171. /* reset the state from a help perspective */
  172. this->state = SHELL_STATE_READY;
  173. }
  174. return result;
  175. }
  176. /*--------------------------------------------------------- */