shell_command_generator.c 5.3 KB

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