1
0

shell_command_generator.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. const char *name = clish_command__get_name(cmd);
  54. char *text = lub_string_dup(&line[offset]);
  55. clish_ptype_t *ptype;
  56. unsigned idx = lub_argv_wordcount(name);
  57. /* get the index of the current parameter */
  58. unsigned index = lub_argv_wordcount(line) - idx;
  59. if ((0 != index) || (line[offset - 1] == ' ')) {
  60. lub_argv_t *argv = lub_argv_new(line, 0);
  61. clish_pargv_t *pargv = clish_pargv_create();
  62. clish_pargv_t *completion_pargv = clish_pargv_create();
  63. unsigned completion_index = 0;
  64. const clish_param_t *param = NULL;
  65. /* if there is some text for the parameter then adjust the index */
  66. if ((0 != index) && (text[0] != '\0'))
  67. index--;
  68. /* Parse command line to get completion pargv's */
  69. clish_pargv_parse(pargv, cmd, this->viewid,
  70. clish_command__get_paramv(cmd),
  71. argv, &idx, completion_pargv, index + idx);
  72. clish_pargv_delete(pargv);
  73. lub_argv_delete(argv);
  74. while ((param = clish_pargv__get_param(completion_pargv,
  75. completion_index++))) {
  76. char *result;
  77. /* The param is args so it has no completion */
  78. if (param == clish_command__get_args(cmd))
  79. continue;
  80. /* The switch has no completion string */
  81. if (CLISH_PARAM_SWITCH == clish_param__get_mode(param))
  82. continue;
  83. /* The subcommand is identified by it's value */
  84. if (CLISH_PARAM_SUBCOMMAND ==
  85. clish_param__get_mode(param)) {
  86. result = clish_param__get_value(param);
  87. if (result)
  88. lub_argv_add(matches, result);
  89. }
  90. /* The common PARAM. Let ptype do the work */
  91. if ((ptype = clish_param__get_ptype(param)))
  92. clish_ptype_word_generator(ptype, matches, text);
  93. }
  94. clish_pargv_delete(completion_pargv);
  95. }
  96. lub_string_free(text);
  97. }
  98. /*--------------------------------------------------------- */