shell_command_generator.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, clish_shell_iterator_t * iter)
  19. {
  20. const clish_command_t *result, *cmd;
  21. /* ask the local view for next command */
  22. result = clish_view_find_next_completion(this->view,
  23. iter->last_cmd, line, iter->field, BOOL_TRUE);
  24. /* ask the global view for next command */
  25. cmd = clish_view_find_next_completion(this->global,
  26. iter->last_cmd, line, iter->field, BOOL_TRUE);
  27. if (clish_command_diff(result, cmd) > 0)
  28. result = cmd;
  29. if (!result)
  30. iter->last_cmd = NULL;
  31. else
  32. iter->last_cmd = clish_command__get_name(result);
  33. return result;
  34. }
  35. /*--------------------------------------------------------- */
  36. const clish_command_t *clish_shell_getfirst_command(clish_shell_t * this,
  37. const char *line, clish_nspace_visibility_t field)
  38. {
  39. clish_shell_iterator_init(&this->context.iter, field);
  40. /* find the first command for which this is a prefix */
  41. return clish_shell_getnext_command(this, line);
  42. }
  43. /*--------------------------------------------------------- */
  44. const clish_command_t *clish_shell_getnext_command(clish_shell_t * this,
  45. const char *line)
  46. {
  47. return clish_shell_find_next_completion(this, line, &this->context.iter);
  48. }
  49. /*--------------------------------------------------------- */
  50. void clish_shell_param_generator(clish_shell_t *this, lub_argv_t *matches,
  51. const clish_command_t *cmd, const char *line, unsigned offset)
  52. {
  53. char *result = NULL;
  54. const char *name = clish_command__get_name(cmd);
  55. char *text = lub_string_dup(&line[offset]);
  56. clish_ptype_t *ptype;
  57. unsigned idx = lub_argv_wordcount(name);
  58. /* get the index of the current parameter */
  59. unsigned index = lub_argv_wordcount(line) - idx;
  60. if ((0 != index) || (line[offset - 1] == ' ')) {
  61. lub_argv_t *argv = lub_argv_new(line, 0);
  62. clish_pargv_t *pargv = clish_pargv_create();
  63. clish_pargv_t *completion_pargv = clish_pargv_create();
  64. unsigned completion_index = 0;
  65. const clish_param_t *param = NULL;
  66. /* if there is some text for the parameter then adjust the index */
  67. if ((0 != index) && (text[0] != '\0'))
  68. index--;
  69. /* Parse command line to get completion pargv's */
  70. clish_pargv_parse(pargv, cmd, this->viewid,
  71. clish_command__get_paramv(cmd),
  72. argv, &idx, completion_pargv, index + idx);
  73. clish_pargv_delete(pargv);
  74. lub_argv_delete(argv);
  75. while ((param = clish_pargv__get_param(completion_pargv,
  76. completion_index++))) {
  77. if (param == clish_command__get_args(cmd)) {
  78. /* The param is args so it has no completion */
  79. result = NULL;
  80. } else if (CLISH_PARAM_SWITCH ==
  81. clish_param__get_mode(param)) {
  82. /* The switch has no completion string */
  83. result = NULL;
  84. } else if (CLISH_PARAM_SUBCOMMAND ==
  85. clish_param__get_mode(param)) {
  86. /* The subcommand is identified by it's value */
  87. result = clish_param__get_value(param);
  88. } else {
  89. /* The common PARAM. Let ptype do the work */
  90. if ((ptype = clish_param__get_ptype(param)))
  91. clish_ptype_word_generator(ptype,
  92. matches, text);
  93. else
  94. result = NULL;
  95. }
  96. if (result)
  97. lub_argv_add(matches, result);
  98. }
  99. clish_pargv_delete(completion_pargv);
  100. }
  101. lub_string_free(text);
  102. }
  103. /*--------------------------------------------------------- */