1
0

shell_command_generator.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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,
  46. unsigned state)
  47. {
  48. char *result = NULL;
  49. const char *name = clish_command__get_name(cmd);
  50. char *text = lub_string_dup(&line[offset]);
  51. unsigned index;
  52. const clish_param_t *param = NULL;
  53. clish_ptype_t *ptype;
  54. unsigned idx;
  55. /* get the index of the current parameter */
  56. index = lub_argv_wordcount(line) - lub_argv_wordcount(name);
  57. if ((0 != index) || (line[offset - 1] == ' ')) {
  58. if (0 == state) {
  59. lub_argv_t *argv;
  60. clish_pargv_t *pargv;
  61. unsigned i;
  62. if ((0 != index) && (text[0] != '\0')) {
  63. /* if there is some text for the parameter then adjust the index */
  64. index--;
  65. }
  66. argv = lub_argv_new(line, 0);
  67. idx = lub_argv_wordcount(name);
  68. if (this->context.completion_pargv) {
  69. clish_pargv_delete(this->context.completion_pargv);
  70. this->context.completion_pargv = NULL;
  71. }
  72. this->context.completion_pargv = clish_pargv_create();
  73. pargv = clish_pargv_create();
  74. clish_pargv_parse(pargv, cmd, this->viewid, clish_command__get_paramv(cmd),
  75. argv, &idx, this->context.completion_pargv, index + idx);
  76. clish_pargv_delete(pargv);
  77. lub_argv_delete(argv);
  78. this->context.completion_index = 0;
  79. this->context.completion_pindex = 0;
  80. }
  81. while ((param = clish_pargv__get_param(this->context.completion_pargv,
  82. this->context.completion_index++))) {
  83. if (param == clish_command__get_args(cmd)) {
  84. /* The param is args so it has no format */
  85. result = lub_string_dup(text);
  86. } else if (CLISH_PARAM_SUBCOMMAND ==
  87. clish_param__get_mode(param)) {
  88. /* The subcommand is identified by it's value */
  89. result = lub_string_dup(clish_param__get_value(param));
  90. } else if (CLISH_PARAM_SWITCH ==
  91. clish_param__get_mode(param)) {
  92. /* The switch has no completion string */
  93. result = NULL;
  94. } else {
  95. /* The common param. Let ptype do the work */
  96. if (ptype = clish_param__get_ptype(param)) {
  97. result = clish_ptype_word_generator(ptype, text,
  98. this->context.completion_pindex++);
  99. if (!result)
  100. this->context.completion_pindex = 0;
  101. else
  102. this->context.completion_index--;
  103. } else {
  104. result = NULL;
  105. }
  106. }
  107. if (result)
  108. break;
  109. }
  110. } else if (0 == state) {
  111. /* simply return the command name */
  112. result = lub_string_dup(clish_command__get_suffix(cmd));
  113. }
  114. if (!result) {
  115. clish_pargv_delete(this->context.completion_pargv);
  116. this->context.completion_pargv = NULL;
  117. /* make sure we reset the line state */
  118. // tinyrl_crlf(this->tinyrl);
  119. // tinyrl_reset_line_state(this->tinyrl);
  120. // tinyrl_completion_error_over(this->tinyrl);
  121. }
  122. lub_string_free(text);
  123. return result;
  124. }
  125. /*--------------------------------------------------------- */
  126. static char *clish_shell_command_generator(clish_shell_t * this,
  127. const char *line,
  128. unsigned offset, unsigned state)
  129. {
  130. char *result = NULL;
  131. const clish_command_t *cmd = NULL;
  132. if (0 == state) {
  133. cmd =
  134. clish_shell_getfirst_command(this, line,
  135. CLISH_NSPACE_COMPLETION);
  136. } else {
  137. cmd = clish_shell_getnext_command(this, line);
  138. }
  139. if (NULL != cmd) {
  140. result = lub_string_dup(clish_command__get_suffix(cmd));
  141. }
  142. /* keep the compiler happy */
  143. offset = offset;
  144. return result;
  145. }
  146. /*--------------------------------------------------------- */
  147. char *clish_shell_word_generator(clish_shell_t * this,
  148. const char *line,
  149. unsigned offset, unsigned state)
  150. {
  151. char *result = NULL;
  152. const clish_command_t *cmd, *next = NULL;
  153. /* try and resolve a command which is a prefix of the line */
  154. cmd = clish_shell_resolve_command(this, line);
  155. if (NULL != cmd) {
  156. clish_shell_iterator_t iter;
  157. /* see whether there is an extended extension */
  158. clish_shell_iterator_init(&iter, CLISH_NSPACE_COMPLETION);
  159. next = clish_shell_find_next_completion(this, line, &iter);
  160. }
  161. if ((NULL != cmd) && (NULL == next)) {
  162. /* this needs to be completed as a parameter */
  163. result =
  164. clish_shell_param_generator(this, cmd, line, offset,
  165. state);
  166. } else {
  167. /* this needs to be completed as a command */
  168. result =
  169. clish_shell_command_generator(this, line, offset, state);
  170. }
  171. if (0 == state) {
  172. /* reset the state from a help perspective */
  173. this->state = SHELL_STATE_READY;
  174. }
  175. return result;
  176. }
  177. /*--------------------------------------------------------- */